Chapter 2 Basic Lisp


If you are used to the standard C syntax of other languages, you'll probably find Lisp syntax strange at the beginning. Don't worry, you'll be used to it quickly.

A brief explanation of S-Exprs

Lisp has a very minimalistic syntax. Look at this code:

(sqrt (+ 2 3))

It calls two functions, sqrt and +. Like in maths, it first evaluates the inner parenthesis. Next, the outer ones.

The syntax is always the same: first there is a parenthesis, next there is the name of a function to call, next there are the arguments to that function. Even constructors, which are inherent to the language (like defining functions, declaring variables, and all kind of operations) are always defined the same way. First the action, next the arguments. Remember that.

Although this way of expressing programs might seem to be strange at first glance, it is easy to get used very fast and easy. And because the syntax is very consistent, there are little possibilities to make syntax mistakes at any point.

A function definition in PicoLisp? Have a look:

(de fib (N)
   (if (> 2 N)
      (+ (fib (dec N)) (fib (- N 2))) ) )

You don't need to know what it does for the moment, we'll cover the details later.


The secrets for reading and writing S-Exprs are in the indentation and parenthesis matching highlight. If you look at the code above, every time that a significant construction or function call happens, it is written in another line, and arguments at the right. Every modern editor today is capable of doing both, so don't worry.

S-Expr for everything: a direct comparison

S-Eprs have several advantages, compared to others syntaxes, like C (and derivatives) one. We'll try to cover the most important below.

Shorter code

Test this in the REPL:

(+ 1 2 3 4 5)

As you can see, it is only the needed to write the '+' once. Compare that with:

1 + 2 + 3 + 4 + 5

Not only it is larger, but your eyes might get confused at some point when reading the expression.

You don't need to learn precedence rules


(+ (* 2 3 4)
   (/ 8 2)
   (if (< 2 8)
      (+ 4 3)
      (- 4 3) ) )


2 * 3 * 4 + 8 / 2 + 2 < 8 ? 4 + 3 : 4 - 3

The C version is just slightly shorter (and just because we use arithmetic functions) but it is very error prone if you don't exactly know the operator preference rules. Even if you know, it if difficult to read, so you end up writing parenthesis:

(2 * 3 * 4) + (8 / 2) + (2 < 8 : (4 + 3) : (4 - 3))

Even in that case, the S-Expr way is easier to read, specially when you are used to it. Also, in C it is not very idiomatic to split expressions in several lines unless you reach the end of line, because it is not very idiomatic. C doesn't encourage beauty code. Lisp does.

Code as data

Because the syntax is minimal and completely consistent all the time (no need to exceptions), programs can be treated as data, and data as programs. We'll talk deeper about this in future chapters.
This is by far the main reason because S-Exprs are so convenient, and the principal characteristic of Lisp. Can you imagine a program that can produce programs at run time? Can you imagine programs that can modify themselves at run time? That's the power of S-Exprs and Lisp.

Elegant code

This is a subjective matter, but very important because it makes you feel comfortable writing and reading code. There is no place for ugliness, as all the syntax is the same. No need to learn obscure facilities that are there only for the purpose to make the brain of a hacker happier (i.e. Perl). Languages should be for people to read, and eventually for computers to execute.

You don't have to believe, try it!

Everybody has some reservations the first time they entered in the Lisp world, and are afraid about all that parenthesis. There are good news if you are one of them: everybody get used to it in not very long time. Yo can expect being comfortable with it in about two days, if you write some simple functions while reading this book. You can reach the level of falling in love with Lisp in about two months of using PicoLisp for small programs or experimentation. You'll probably prefer it over any other language in about two years of constantly using it. It happened us, it shall happen to you if you continue using Lisp.

Simple operations

Please start the interpreter as explained in last chapter so you can experiment as we go on.

Mathematical operations

We've already use some mathematical forms. Experiment with this examples, and invent others, until you feel a little more comfortable with both the interpreter and the way S-Exprs are entered:

: (+ 1 2 3 4)
: (+ (* 4 5)
:    (* 8 6 (/ 6 3)) )
: (% 6 4)  # % refers to the function "rest of a division"
: (sqrt 9)  # sqrt is the function square root
: (+ (rand 0 10) 10)  # rand is the function that returns a random number between its arguments

Look at the manual, the section about functions reference, and experiment with the functions located there.

Fixed point math

PicoLisp does not have floating point numbers, but that doesn't mean you can't use real numbers. In fact, there is a wrapper for OpenGl using

Unless otherwise stated, the content of this page is licensed under Creative Commons Attribution-NonCommercial-ShareAlike 3.0 License