Experiments in controlling LEDs using Guile + Arduino
[clinton/scratch.git] / automata.scm
CommitLineData
0bbcd8e7
CE
1(use-modules (srfi srfi-9)
2 (srfi srfi-1))
3
4(define (next-token stream)
5 (if (pair? stream)
6 (car stream)))
7
8(define (transition machine state)
9 ((cdr (assoc state (cdr machine)))
10 (cons (if (pair? (car machine)) (cdr (car machine)))
11 (cdr machine))
12 (next-token (car machine))))
13
14(define (run-machine machine initial-state stream)
15 (transition (cons stream machine) initial-state))
16
17(define cadr-machine
18 `((init . ,(lambda (m v) (case v
19 ((#\c) (transition m 'more))
20 (else #f))))
21 (more . ,(lambda (m v) (case v
22 ((#\a #\d) (transition m 'more))
23 ((#\r) (transition m 'end))
24 (else #f))))
25 (end . ,(lambda (m v) #t))))
26
27