Advertisement
Guest User

HtDP exercise 476

a guest
Aug 25th, 2020
72
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Racket 2.50 KB | None | 0 0
  1. (define-struct transition [current key next])
  2. (define-struct fsm [initial transitions final])
  3.  
  4. ; An FSM is a structure:
  5. ;   (make-fsm FSM-State [List-of 1Transition] FSM-State)
  6. ; A 1Transition is a structure:
  7. ;   (make-transition FSM-State 1String FSM-State)
  8. ; An FSM-State is String.
  9.  
  10. ; data example: see exercise 109
  11.  
  12. (define fsm-a-bc*-d
  13.   (make-fsm
  14.    "AA"
  15.    (list (make-transition "AA" "a" "BC")
  16.          (make-transition "BC" "b" "BC")
  17.          (make-transition "BC" "c" "BC")
  18.          (make-transition "BC" "d" "DD"))
  19.    "DD"))
  20.  
  21. ; FSM String -> Boolean
  22. ; does an-fsm recognize the given string
  23. ; generative: if the first letter in a-string is a valid input for the initial state, creates a new
  24. ; FSM with the resulting state and checks that the next letter is a valid input for that, until it
  25. ; reaches the case where the remaining letter has to make the transition to the final state
  26. (check-expect
  27.  (fsm-match? fsm-a-bc*-d "ad") #true)
  28. (check-expect
  29.  (fsm-match? fsm-a-bc*-d "abcd") #true)
  30. (check-expect
  31.  (fsm-match? fsm-a-bc*-d "acbd") #true)
  32. (check-expect
  33.  (fsm-match? fsm-a-bc*-d "abcbcbcbcbcd") #true)
  34. (check-expect
  35.  (fsm-match? fsm-a-bc*-d "aa") #false)
  36. (check-expect
  37.  (fsm-match? fsm-a-bc*-d "d") #false)
  38. (check-expect
  39.  (fsm-match? fsm-a-bc*-d "da") #false)
  40.  
  41. (define (fsm-match? an-fsm a-string)
  42.   (local ((define transitions (fsm-transitions an-fsm))
  43.           (define current-state (fsm-initial an-fsm))
  44.           (define current-key (substring a-string 0 1))
  45.           ; [List-of 1Transition] -> [Maybe FSM-State]
  46.           ; returns the FSM-State that results from pressing the current key in the current state
  47.           ; if a transition is defined for it, #false otherwise
  48.           (define (check-key tr)
  49.             (cond
  50.               [(empty? tr) #false]
  51.               [else
  52.                (local ((define fst (first tr)))
  53.                  (if (and (string=? current-state (transition-current fst))
  54.                           (string=? current-key (transition-key fst)))
  55.                      (transition-next fst)
  56.                      (check-key (rest tr))))]))
  57.           (define maybe-next (check-key transitions)))
  58.     (cond
  59.       [(= 1 (string-length a-string))
  60.        (equal? (fsm-final an-fsm) maybe-next)]
  61.       [else
  62.        (if (string? maybe-next)
  63.            (fsm-match?
  64.             (make-fsm maybe-next
  65.                       transitions
  66.                       (fsm-final an-fsm))
  67.             (substring a-string 1 (string-length a-string)))
  68.            #false)])))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement