
(define states '(INIT SINE SAW PAUSE EXIT))

(define arrows
  '((INIT "sine" SINE) (INIT "saw" SAW) (INIT "quit" EXIT)
    (SINE "saw" SAW)
    (SINE "quit" EXIT)
    (SINE "exit" EXIT)
    (SINE "pause" PAUSE)
    (PAUSE "sine" SINE)
    (PAUSE "saw" SAW)
    (SAW "sine" SINE)
    (SAW "pause" PAUSE)
    (SAW "quit" EXIT)
    (SAW "exit" EXIT)))


(define (findNextState currentState message arrows)
  (if (empty? arrows) currentState
    (if (and
          (eq? currentState (car (car arrows)))
	  (string=? message (cadr (car arrows))))
	  (caddr (car arrows)) ; return next state
      (findNextState currentState message (cdr arrows)))))

; (findNextState 'INIT "saw" arrows)
; (findNextState 'INIT "sine" arrows)
; (findNextState 'SAW "quit" arrows)

(define (updateStateMachine (state 'INIT) (message "init"))
  (define next-state (findNextState state message arrows))
    (if (eq? next-state 'EXIT) (display "DONE")
      (begin
        (display next-state)
        (updateStateMachine next-state (read-line) ))))

(updateStateMachine 'INIT)

