Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <h2>Labels and named lets if your Lisp optimizes tail calls</h2> <p>Just as a matter of playing "stupid Lisp tricks", I'd point out that in Scheme, the analog to </p> <pre><code>sum a b = let loop s i = if i &gt; b then sum else loop (s + i) (i + 1) in loop 0 a </code></pre> <p>is letrec or a named let:</p> <pre><code>(define (sum a b) (letrec ((loop (lambda (s i) (if (&gt; i b) s (loop (+ s i) (+ i 1)))))) (loop 0 a))) (define (sum a b) (let loop ((s 0) (i a)) (if (&gt; i b) s (loop (+ s i) (+ i 1))))) </code></pre> <p>Letrec, because Scheme is a Lisp-1, gives you the functionality of <code>labels</code>, which <a href="https://stackoverflow.com/a/19535069/1281433">Baggers described</a>. The named let can be done in Common Lisp with a macro around <code>labels</code>:</p> <pre><code>(defmacro named-let (name bindings &amp;body body) `(labels ((,name ,(mapcar 'first bindings) ,@body)) (,name ,@(mapcar 'second bindings)))) (pprint (macroexpand '(named-let loop ((s 0) (i a)) (if (&gt; i b) s (loop (+ s i) (+ i 1)))))) ;; (LABELS ((LOOP (S I) ;; (IF (&gt; I B) ;; S ;; (LOOP (+ S I) ;; (+ I 1))))) ;; (LOOP 0 A)) </code></pre> <h2>Do and Do* should be efficient everywhere</h2> <p>However, tail calls aren't necessarily optimized away in Common Lisp, so this kind of recursion for iteration isn't all that common. The iterative analog is <code>do</code>:</p> <pre><code>(defun sum (a b) (do ((s 0 (+ s i)) (i a (+ i 1))) ((&gt; i b) s))) </code></pre> <h2>Loop works too</h2> <p>You can use <code>loop</code> too, but it's a bit more verbose (but also probably more readable if you're familiar with <code>do</code>:</p> <pre><code>(defun sum (a b) (loop for s = 0 then (+ s i) for i = a then (+ i 1) when (&gt; i b) return s)) </code></pre>
    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