|
|
Line 1,291: |
Line 1,291: |
|
true . |
|
true . |
|
</pre> |
|
</pre> |
|
|
|
⚫ |
|
|
⚫ |
<lang racket>#lang racket |
|
|
|
|
⚫ |
|
|
⚫ |
'((ready (deposit . waiting) |
|
⚫ |
|
|
⚫ |
(waiting (select . dispense) |
|
⚫ |
|
|
⚫ |
(dispense (remove . ready)) |
|
⚫ |
|
|
|
|
|
⚫ |
(define (machine states prompt get-action quit) |
|
⚫ |
(let recur ((state (caar states))) |
|
⚫ |
(printf "CURRENT STATE: ~a~%" state) |
|
⚫ |
|
|
⚫ |
|
|
⚫ |
(recur (match (cdr (assoc state states)) |
|
⚫ |
[(list (and transitions (cons actions _)) ...) |
|
⚫ |
(prompt "next action (from: ~a): " actions) |
|
⚫ |
(match (assoc (get-action) transitions) |
|
⚫ |
|
|
⚫ |
(printf "~a -> ~a -> ~a~%" state action new-state) |
|
⚫ |
|
|
⚫ |
[#f (printf "invalid action for~%") state])] |
|
⚫ |
|
|
⚫ |
(printf "~a -> ~a~%" state auto-state) |
|
⚫ |
|
|
|
|
|
⚫ |
|
|
⚫ |
|
|
⚫ |
(with-input-from-string "deposit select remove deposit refund quit" |
|
⚫ |
(λ () (machine states void read quit)))))</lang> |
|
⚫ |
|
|
⚫ |
<pre>CURRENT STATE: ready |
|
⚫ |
ready -> deposit -> waiting |
|
⚫ |
|
|
⚫ |
waiting -> select -> dispense |
|
⚫ |
|
|
⚫ |
dispense -> remove -> ready |
|
⚫ |
|
|
⚫ |
ready -> deposit -> waiting |
|
⚫ |
|
|
⚫ |
waiting -> refund -> refunding |
|
⚫ |
|
|
⚫ |
|
|
⚫ |
|
|
⚫ |
|
|
⚫ |
CURRENT STATE: exit</pre> |
|
|
|
|
|
=={{header|Python}}== |
|
=={{header|Python}}== |
|
{{works with|Python 3}} |
|
{{works with|Python 3}} |
Line 1,415: |
Line 1,365: |
|
PS C:\alan\programming> |
|
PS C:\alan\programming> |
|
</pre> |
|
</pre> |
|
|
|
|
⚫ |
|
|
⚫ |
<lang racket>#lang racket |
|
|
|
|
⚫ |
|
|
⚫ |
'((ready (deposit . waiting) |
|
⚫ |
|
|
⚫ |
(waiting (select . dispense) |
|
⚫ |
|
|
⚫ |
(dispense (remove . ready)) |
|
⚫ |
|
|
|
|
|
⚫ |
(define (machine states prompt get-action quit) |
|
⚫ |
(let recur ((state (caar states))) |
|
⚫ |
(printf "CURRENT STATE: ~a~%" state) |
|
⚫ |
|
|
⚫ |
|
|
⚫ |
(recur (match (cdr (assoc state states)) |
|
⚫ |
[(list (and transitions (cons actions _)) ...) |
|
⚫ |
(prompt "next action (from: ~a): " actions) |
|
⚫ |
(match (assoc (get-action) transitions) |
|
⚫ |
|
|
⚫ |
(printf "~a -> ~a -> ~a~%" state action new-state) |
|
⚫ |
|
|
⚫ |
[#f (printf "invalid action for~%") state])] |
|
⚫ |
|
|
⚫ |
(printf "~a -> ~a~%" state auto-state) |
|
⚫ |
|
|
|
|
|
⚫ |
|
|
⚫ |
|
|
⚫ |
(with-input-from-string "deposit select remove deposit refund quit" |
|
⚫ |
(λ () (machine states void read quit)))))</lang> |
|
⚫ |
|
|
⚫ |
<pre>CURRENT STATE: ready |
|
⚫ |
ready -> deposit -> waiting |
|
⚫ |
|
|
⚫ |
waiting -> select -> dispense |
|
⚫ |
|
|
⚫ |
dispense -> remove -> ready |
|
⚫ |
|
|
⚫ |
ready -> deposit -> waiting |
|
⚫ |
|
|
⚫ |
waiting -> refund -> refunding |
|
⚫ |
|
|
⚫ |
|
|
⚫ |
|
|
⚫ |
|
|
⚫ |
CURRENT STATE: exit</pre> |
|
|
|
|
=={{header|Raku}}== |
|
=={{header|Raku}}== |
|
(formerly Perl 6) |
|
(formerly Perl 6) |