Skip to content

Your first program

In this tutorial you will write a small expense report and run it as a script. You need a working egcl command on your PATH; if you do not have one, build and install it first.

Evaluate an expression

In a terminal, run:

egcl --no-init --eval '(+ 12 8 5)'

EGCL prints 25 and exits.

Define a function in the REPL

Start the interactive read–eval–print loop:

egcl --no-init

At the CL-USER> prompt, enter:

(defun total (amounts)
  (reduce #'+ amounts :initial-value 0))

(total '(12 8 5))

The function definition returns its name. The next expression returns 25. You can call the function with another list without redefining it:

(total '(4 6))

This returns 10. Enter (quit) to leave the REPL.

Put the program in a file

Create expenses.lisp with this content:

(defun total (amounts)
  (reduce #'+ amounts :initial-value 0))

(format t "~A: ~D~%"
        (first *command-line-args*)
        (total '(12 8 5)))

Run the file, passing a report label after --:

egcl expenses.lisp -- Lunch

The program prints:

Lunch: 25

You now have a Lisp script that takes input from the command line and produces formatted output. To distribute an executable containing your loaded program, continue with Save an executable.