How to Read program arguments in Lisp

Lisp is a completely different way of looking at programming than e.g. ruby is. Where in Ruby everything is an object - in lisp everything is a list. Which makes sense, since lisp is an acronym for LISt Processing.

To get program arguments in lisp is not something uniform - each lisp dialect has it’s own way of doing it. For an example covering if not all, then many of the dialects, see this listing at rosettacode.org.

The code below is for the dialect called clisp.

(setq counter 0)
(loop for e in *args* collect
  (progn
    (setq counter (+ counter 1))
    (write-line (concatenate 'string "argument #" (write-to-string counter) ": "  e))
    ))

First we define a variable counter, and initialize it to zero.

Then we loop through all the elements (e) in the program arguments (args). The collect call runs the “callback”. This is made a list of commands with progn instead of just a single command. This lets us increment the counter, before concating all the string elements into a final string, which is then printed to stdout.

Related Posts