💾 Archived View for thebird.nl › gn-gemtext-threads › topics › lisp › lisp4schemers.gmi captured on 2023-05-24 at 18:31:57. Gemini links have been rewritten to link to archived content

View Raw

More Information

⬅️ Previous capture (2022-04-28)

-=-=-=-=-=-=-

Lisp For Schemers

Tags

iota

In Scheme

scheme@(guile-user)> (iota 12)

$1 = (0 1 2 3 4 5 6 7 8 9 10 11)

In Common Lisp

The Common Lisp loop macro is not without its problems. See the paper "Don't Loop, Iterate" linked below. The paper compares different iteration mechanisms and promotes the author's own iterate macro. Personally, I think Scheme's approach with SRFI-1 is much cleaner. Explicit iteration is often distracting. Recursion is often what the programmer really wants.

Don't Loop, Iterate

CL-USER: (loop for i below 12 collect i)

(0 1 2 3 4 5 6 7 8 9 10 11)

CL-USER: (macroexpand '(loop for i below 12 collect i))

(BLOCK NIL
  (LET ((I 0))
    (DECLARE (IGNORABLE I)
             (TYPE (AND NUMBER REAL) I))
    (SB-LOOP::WITH-LOOP-LIST-COLLECTION-HEAD (#:LOOP-LIST-HEAD-493
                                              #:LOOP-LIST-TAIL-494)
      (TAGBODY
       SB-LOOP::NEXT-LOOP
        (WHEN (>= I '12) (GO SB-LOOP::END-LOOP))
        (SB-LOOP::LOOP-COLLECT-RPLACD
         (#:LOOP-LIST-HEAD-493 #:LOOP-LIST-TAIL-494) (LIST I))
        (SB-LOOP::LOOP-DESETQ I (1+ I))
        (GO SB-LOOP::NEXT-LOOP)
       SB-LOOP::END-LOOP
        (RETURN-FROM NIL
          (SB-LOOP::LOOP-COLLECT-ANSWER #:LOOP-LIST-HEAD-493))))))
T

Using iota from the alexandria library with GNU/Guix:

guix shell sbcl sbcl-alexandria rlwrap -- rlwrap sbcl

CL-USER: (asdf:make "alexandria")

CL-USER: (in-package :alexandria)

CL-USER: (alexandria:iota 12)

(0 1 2 3 4 5 6 7 8 9 10 11)

alexandria