A Tiny Lisp Script

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' and
incrementing 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

3 thoughts on “A Tiny Lisp Script

  1. keramida Post author

    foo In general, I would agree, but not in this case.

    #'iota expects 3 arguments, and #'reduce would do something slightly odd; it would pass only the first two *posix-argv* elements, then pass the result and the third *posix-argv* element, and so on…

Comments are closed.