close
  • person chevron_right

    DOTIMES and DOLIST in Lisp Flavored Erlang

    Michał "phoe" Herda – Sunday, 23 December, 2018 - 11:59

(This is a repost of an old blog post of mine from Teknik.)

#lfe #macros

No unnecessary talking - here are CL:DOLIST and CL:DOTIMES macros reimplemented in LFE. CL:DOTIMES is awful, but works well in face on the GENSYM-less environment of LFE.

(defmacro dolist args
  (let ((`((,var ,list . ,result) . ,body) args))
    `(do ((,var ,list (cdr ,var)))
         ((== '() ,var) ,result)
       (let ((,var (car ,var)))
         ,@body))))
(defmacro dotimes args
  (let* ((`((,var ,n) . ,body) args)
         (range (lists:seq 0 (- n 1)))
         (fn (lambda (i) `(let ((,var ,i)) ,@body)))
         (result (cl:mapcar fn range)))
    `(progn ,@result)))