Skip to content

Instantly share code, notes, and snippets.

@ehaliewicz
Last active December 10, 2015 00:19
Show Gist options
  • Save ehaliewicz/4350229 to your computer and use it in GitHub Desktop.
Save ehaliewicz/4350229 to your computer and use it in GitHub Desktop.
(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