A Tiny Lisp Script

25 Feb 2009, 03:18 by Giorgos Keramidas

A tiny Lisp script, noticed first in a recent post in comp.lang.lisp and then ported to SBCL:

    % cat ~/bin/iota
    #!/usr/local/bin/sbcl --script
    (defun iota (count &optional (start 0) (step 1))
      "Build a list of `count' numbers, starting from `start' andincrementing by `step' between
      successive elements."

      (loop repeat count for i from start by step
        collect i))

    (format t "~{~a~^ ~}~%"
            (apply 'iota (mapcar #'read-from-string (rest *posix-argv*))))

Running this from a shell prompt works fine in SBCL 1.0.25:

    % iota 10
    0 1 2 3 4 5 6 7 8 9
    % iota 10 1
    1 2 3 4 5 6 7 8 9 10

It even works with float step values, which is a bit cool:

    % iota 10 0 1d-2
    0 0.01d0 0.02d0 0.03d0 0.04d0 0.05d0 0.06d0 0.07d0 0.08d0 0.09d0

Comments