Skip to content

Instantly share code, notes, and snippets.

@toctan
Created June 21, 2014 08:48
Show Gist options
  • Save toctan/3d8e63448c99556ccd9f to your computer and use it in GitHub Desktop.
Save toctan/3d8e63448c99556ccd9f to your computer and use it in GitHub Desktop.
(defmacro for (var start stop &rest body)
(let ((gstop (gensym)))
`(do ((,var ,start (1+ ,var))
(,gstop ,stop))
((> ,var ,gstop))
,@body)))
(defmacro in (obj &rest choices)
(let ((insym (gensym)))
`(let ((,insym ,obj))
(or ,@(mapcar #'(lambda (c) `(eql ,insym ,c))
choices)))))
(defmacro random-choice (&rest exprs)
`(case (random ,(length exprs))
,@(let ((key -1))
(mapcar #'(lambda (expr)
`(,(incf key) ,expr))
exprs))))
(defmacro avg (&rest args)
`(/ (+ ,@args) ,(length args)))
(defmacro with-gensyms (syms &body body)
`(let ,(mapcar #'(lambda (s)
'(,s (gensym)))
syms)
,@body))
(defmacro aif (test then &optional else)
`(let ((it ,test))
(if it ,then ,else)))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment