Lisp Learning Pt. 1

Feb 3, 2024

In my free time (which I have very little), I’m learning the Lisp programming language. After doing some search, I decided to go with Common Lisp and installed SBCL.

Unless you're using Emacs, do not write more than few lines of code in REPL (in Terminal). Most of the terminals don't support parenthesis auto-closing and highlighting. Due to this, you might be frustrated.

I’m using a GNU/Linux machine. Following command helped me to install the SBCL on my computer.

sudo apt install sbcl

After installation, check for the version and you can confirm the installation.

sbcl --version

Write only sbcl in the Terminal and it’ll open the true REPL for you. In order to quit or exit the REPL you need to write (quit).

$ sbcl
This is SBCL 2.2.9.debian, an implementation of ANSI Common Lisp.
More information about SBCL is available at <http://www.sbcl.org/>.

SBCL is free software, provided as is, with absolutely no warranty.
It is mostly in the public domain; some portions are provided under
BSD-style licenses.  See the CREDITS and COPYING files in the
distribution for more information.
* 

* is the prompt. You can type the Lisp code now.

You can directly write 10 or "hello, world" (use double quotes) as these are the literal values.

* 10
10
* "hello, world"
"hello, world"

If you try to write 10 as (10) or "hello, world" as ("hello, world") you’ll get errors. For example,

* (10)
; in: 10
;     (10)
; 
; caught ERROR:
;   illegal function call
; 
; compilation unit finished
;   caught 1 ERROR condition

debugger invoked on a SB-INT:COMPILED-PROGRAM-ERROR in thread
#<THREAD "main thread" RUNNING {1001348003}>:
  Execution of a form compiled with errors.
Form:
  (10)
Compile-time error:
  illegal function call

Type HELP for debugger help, or (SB-EXT:EXIT) to exit from SBCL.

restarts (invokable by number or by possibly-abbreviated name):
  0: [ABORT] Exit debugger, returning to top level.

((LAMBDA ()))
   source: (10)

You’re now in true REPL mode. For now, to exit, type 0 and press Enter.

You got the error because, in simple terms, after open paren, Lisp expects to have something that can execute. For example, (+ 3 4) works as + will execute on 3 and 4 values.

* (+ 3 4)
7

Here is the hello, world in Common Lisp.

* (format t "hello, world")
hello, world
NIL

Here, format print on terminal or t (actually it is standard output) "hello, world" string. You can write code within a file that should have .lisp extension as hello.lisp.

; hello.lisp

(format t "hello, world")

Yes, ; is used to write comment in Lisp.

You can run the hello.lisp program using following command.

sbcl --script hello.lisp
hello, world

Or open sbcl REPL and load the file.

sbcl
* (load "hello.lisp")
hello, world
T
Tags: lisp