Skip to content

Instantly share code, notes, and snippets.

@brianru
Created August 1, 2013 17:40
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save brianru/6133553 to your computer and use it in GitHub Desktop.
Save brianru/6133553 to your computer and use it in GitHub Desktop.
; call: (mktransition '(a more)
; expansion:
; (a (more (cdr str)))
(def mktransition (tn) (list (car tn) (list (last tn) '(cdr str))))
; call:
; (mktransitions '((a more)
; (d more)
; (r end)))
; expansion:
; (a (more (cdr str))
; d (more (cdr str))
; r (end (cdr str)))
(def mktransitions (ts)
(accum accfn ; accum helps splice the result of mktransition
(each x (map1 mktransition ts)
(accfn (car x))
(accfn (last x)))))
; call:
; (mkrule '(more (a more) (d more) (r end)))
; expansion:
; more (fn (str)
; (if (empty str)
; t
; (case (car str)
; a (more (cdr str))
; d (more (cdr str))
; r (end (cdr str)))))
(mac mkrule (r)
(let i r
`(list (car ,i) (fn (str)
(if (empty str) t
(case (car str)
,@(mktransitions (cdr i))))))))
; example call:
; (automaton 'init
; '((init (c more))
; (more (a more)
; (d more)
; (r end))
; (end)))
(mac automaton (i r) `(withr/p ,(map1 [mkrule _] r) ,i))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment