Skip to content

CL Tips

Ashok Khanna edited this page Aug 22, 2021 · 2 revisions

Lazy Evaluation (by JackDaniel)

(defmacro with-lazy-values (bindings &body body)
  (loop for (name form) in bindings
        for variable = (gensym (string name))
        for variable-set-p = (gensym (string name))
        collect variable into variables
        collect variable-set-p into variables
        collect `(,name (if ,variable-set-p
                            ,variable
                            (setf ,variable-set-p t
                                  ,variable ,form)))
          into macrolets
        finally (return `(let ,variables
                           (declare (ignorable ,@variables))
                           (symbol-macrolet ,macrolets
                             ,@body)))))

(with-lazy-values ((a (dotimes (x 3 15)
                        (print "HELLO")))
                   (b (dotimes (x 3 33)
                        (print "bye")))
                   (c (+ a 15)))
  c)

[PJB] you can use (time …) to get a report on the time needed to compute …