Last active
December 10, 2015 00:19
-
-
Save ehaliewicz/4350229 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(defmacro alet (letargs &rest body) | |
`(let ((this) ,@letargs) | |
(setq this ,@(last body)) | |
,@(butlast body) | |
(lambda (&rest params) | |
(apply this params)))) | |
(defmacro alet-fsm (&rest states) | |
`(macrolet ((state (s) | |
`(progn (setq this #',s) | |
t))) | |
(labels (,@states) #',(caar states)))) | |
(defparameter *fsm* | |
(alet ((x 0)) | |
(alet-fsm | |
(left (&optional n) | |
(if (eql n 'right) | |
(progn | |
(format t "turning right~%") | |
(state right)) | |
(progn | |
(format t "going left~%") | |
(format t "X position: ~a" (decf x))))) | |
(right (&optional n) | |
(if (eql n 'left) | |
(progn | |
(format t "turning left~%") | |
(state left)) | |
(progn | |
(format t "going right~%") | |
(format t "X position: ~a" (incf x)))))))) | |
(funcall *fsm*) | |
-> going left | |
-> X position: -1 | |
(funcall *fsm*) | |
-> going left | |
-> X position: -2 | |
(funcall *fsm* 'right) | |
-> turning right | |
(funcall *fsm*) | |
-> going right | |
-> X position: -1 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment