Skip to content

Instantly share code, notes, and snippets.

@ehaliewicz
ehaliewicz / thread.lisp
Last active October 14, 2015 00:58
a simple function threading macro
(defmacro -> (init &rest funcs)
(cond
((null funcs) `,init)
((eql 'if (car funcs))
(let* ((gensym (gensym)))
`(-> (let ((,gensym ,init))
(if ,gensym (-> ,gensym ,(cadr funcs))
(-> ,gensym ,(caddr funcs)))
,@(cdddr funcs)))))
((and (consp (car funcs)) (not (eql 'lambda (caar funcs))))
@ehaliewicz
ehaliewicz / flip-flop.lisp
Last active October 12, 2015 08:27
A flip-flop operator for lisp
(defmacro flip-flop (left right)
"Evaluates to false until the left test is satisfied. Then evaluates to true until the right test is satisfied, etc."
;; create a collision-safe name for our state variable
(let ((state-var (gensym)))
;; create a toplevel variable to keep state of flip-flop
`(progn
(defvar ,state-var nil))
@ehaliewicz
ehaliewicz / tail-recur.lisp
Created October 28, 2012 00:49
Tail-recursion and mutual recursion macros
;; A couple of silly macros for a language that has defmacro but no tail-recursion
;; the mutual recursion macro is even worse, because you can only really 'call' one of the functions
(defmacro recur (arg-pairs &body body)
(let ((arg-names (mapcar #'car arg-pairs)) ;; extract loop variable names
(arg-vals (mapcar #'cadr arg-pairs))) ;; extract start values
;; block to return from
`(block nil