Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>I know this doesn't really answer your question, but I do think it is relevant. In my experience, the type of macro you are attempting to write is a very common one. One problem I have with the way you have approached the problem is that it doesn't handle another common use case: functional composition. </p> <p>I don't have the time to highlight some of the difficulties you will probably encounter <em>using</em> your macro, I will however highlight that, had you built your prime iterator geared towards functional composition, your macro turns out to be extremely simple, avoiding your question altogether.</p> <p>Note: I have slightly modified some of your functions.</p> <pre><code>(defun is-prime (n) (cond ((&lt; n 2) nil) ((= n 2) t) ((evenp n) nil) (t (do ((i 2 (1+ i))) ((= i n) t) (when (or (= (mod n i) 0)) (return nil)))))) (defun next-prime (n) (do ((i n (1+ i))) ((is-prime i) i))) (defun prime-iterator (start-at) (let ((current start-at)) (lambda () (let ((next-prime (next-prime current))) (setf current (1+ next-prime)) next-prime)))) (defun map-primes/iterator (fn iterator end) (do ((i (funcall iterator) (funcall iterator))) ((&gt;= i end) nil) (funcall fn i))) (defun map-primes (fn start end) (let ((iterator (prime-iterator start))) (map-primes/iterator fn iterator end))) (defmacro do-primes ((var start end) &amp;body body) `(map-primes #'(lambda (,var) ,@body) ,start ,end)) </code></pre> <p>I too recommend that you look at <a href="http://series.sourceforge.net/" rel="nofollow noreferrer">Series</a>. The generator pattern is also a very common occurrence in lisp programs. You may also want to look at <a href="http://common-lisp.net/project/alexandria/" rel="nofollow noreferrer">Alexandria</a>, in particular the function ALEXANDRIA:COMPOSE to see what cool stuff you can do with functional composition.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload