Skip to content

Instantly share code, notes, and snippets.

@toctan
Last active August 29, 2015 14:02
Show Gist options
  • Save toctan/0f307c3aef04d30acb1e to your computer and use it in GitHub Desktop.
Save toctan/0f307c3aef04d30acb1e to your computer and use it in GitHub Desktop.
(defmacro ntimes (n &rest body) ; wrong
`(do ((i 0 (1+ i)))
((>= i ,n))
,@body))
(let ((i 10)) ; 10
(ntimes 5 (incf i))
i)
(defmacro ntimes (n &rest body) ; also wrong, but in a different way
(let ((i (gensym)))
`(do ((,i 0 (1+ ,i)))
((>= ,i ,n))
,@body)))
(let ((v 10)) ; .....
(ntimes (decf v)
(princ ".")))
(defmacro ntimes (n &rest body) ; finally, the right version
(let ((i (gensym))
(g (gensym)))
`(let ((,g ,n))
(do ((,i 0 (1+ ,i)))
((>= ,i ,g))
,@body))))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment