[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: something *very* strange (to *me*). Please help!



   From: Pierpaolo Bernardi <bernardp@CLI.DI.Unipi.IT>

   >      (let ((l (do ((i 0 (1+ i)) r) ((= i 3) (nreverse r))
   >              (push #'(lambda () i) r))))
   >        (mapcar #'funcall l))
   
   To understand what's going on, try macroexpanding the DO form.

Agreed.
   
   If you want a different behaviour, you should write exactly what you want.
   
   (let ((l (labels ((fun (i r)
                      (if (= i 3)
                          (nreverse r)
                          (fun (+ i 1) (cons (lambda () i) r)))))
              (fun 0 '()))))
    (mapcar #'funcall l))
         
This code is rather different from the original.  I think the
following captures more succinctly the semantic point between the
original code and what was wanted:

      (let ((l (do ((i 0 (1+ i)) r) ((= i 3) (nreverse r))
                 (let ((j i))  ; Only this form is added. -smh
                   (push #'(lambda () j) r)))))
        (mapcar #'funcall l))