Lisp
The actual name lisp
stands for "list processing" and that is exactly what
lisp actual does. Everything in lisp
is a list. The actual syntax of lisp is
very simple and there are very few rules to remember. The syntax is made up of
S-expressions
. An S-expression is either an atom
or a list. Atoms can be
numbers like: 10, 3.14, 0, or symbols like t
(true), +
, my-variable
. There
is also a special kind of symbol called keywords, which are colon-prefixed
symbols like :thing
or :keyword
. Keywords evaluate to themselves: you can
think of them like enums.
Since everything in lisp
is a just a list code is treated like data and data
is treated like code. For this reason lisp
can be a fun and interesting
programming language to learn because it doesn't limit you or hold you back in
anyway. A lisp
language will actually give you so much freedom that you can
override built in functions in the language and alter how they behave, because
at the end of the day these built in functions are just a list of data. Learning
lisp
can also be an amazing learning experience because it will change the way
you think about code in any other programming language.
If this interests you and you are wondering where to begin learning lisp
one
other important thing to note is that for a language to be a "lisp" it just has
a few simple philosophies to follow. Also keeping in mind that since the
language lets you change anything you want about the language itself there are
in fact many many many different lisp languages. These languages can be
referred to as "the lisp family of languages" and anyone of them can be a fine
starting point. One of the most popular and arguably the most simple lisp
language Common Lisp.
One of the other big draws of lisp style programming is what lisp languages call
the REPL
or "read-eval-print loop". Depending on your implementation of lisp
you installed the repl
may behave slightly differently. In general the
repl can almost be thought of like Unix or Dos shell prompt in that you can type
expressions and cause things to happen. However, instead of reading and
interpreting a line of shell commands, lisp read lisp expressions, evaluates
them according to the rules of lisp, and prints the result. This causes an
endless cycle of reading, evaluating, and printing; thus the name repl. If all
of this sounds boring thats because this is just the top level view of what the
repl is. Once you get deeper into it and start using it to load your existing
codebase into ram you will see that it is a live environment built of the base
version of your chosen lisp implementation that allows you to not only execute
your code but interact with and alter it while your code is running or during a
crash or bug.
When a bug popped up on the Deep Space 1 NASA spacecraft 100 million miles away from Earth the team was able to diagnose and fix the running code using a lisp repl. One of the programmers said the following about the incident: "Debugging a program running on a $100M piece of hardware that is 100 million miles away is an interesting experience. Having a read-eval-print loop running on the spacecraft proved invaluable in finding and fixing the problem."
If you are sold on the idea of wanting to learn lisp style programming after reading this if, but for nothing else, to change the way you think about programming I'll now start to dive into some of the basics.
Before continuing, if this is your first ever experience with lisp and using a lisp repl I would definitely recommend watching something like the following video: https://www.youtube.com/watch?v=0DLdQ6yb7h8 to understand how to interact and code with lisp. This video is going over a plugin for Emacs, however the concepts displayed of using a lisp repl are quite valuable.
General Basics
So even though lisp is a family of languages and they can differ slightly in their syntax some key points of the lisp standard reign true throughout the family.
The first key thing to learn or get your head around is the fact that everything
in lisp returns a value. if you open your preferred lisp repl and just type 10
you will see the repl return 10. Under the hood the repl is interpreting this 10
as a lisp object ie: a list of 10: (10)
and this list evaluates to 10
. Now
let's see lisp handles math equations. If you type the following in your repl:
(+ 6 8)
you will see the output: 14
.
Anything we put into parenthesis is a list and generally in lisp lists are
evaluated by treating the first element in the lisp as the name of a function
and the rest of the elements as expressions. In out case +
was the name of a
function that does addition with the expressions passed to it. This function
performs its task and then returns (14)
and that list returns 14
. For a
deeper example let's look how we can do multiple expression calls. Type the
following: (/ (* 4 7) 2)
and you will see it return 14
. In this example the
/
function was called with the inputs of: (* 4 7)
and (2)
. We then run the
*
function with the inputs: (4)
and (7)
. This returns: (28)
which then
gets pass up our original function call which returns a value of: (14)
.
Form here on you may want to view my notes on lisp languages, here is the a lisp… errr I mean a list of different lisps I have notes on:
- Common Lisp
- ELisp (Emacs Lisp)
Placeholder
Anything below here is placeholder notes for now because I am not sure where I want to put them.
Comments in lisp are dictated with the ;
, however we have a few different ways
to write comments per the lisp standard:
;;;;
- Only used to describe your program.
;;;
- A basic comment.
;;
- A comment that is indented with your coded.
;
- A comment that comes after a line of code.
We also have multi line comments in lisp which are written like this:
#|| Multline ||#
The first big take away you need to get your head around when you learn lisp is that lisp uses prefix notation whereas most languages use infix notation. What that means is this:
1 + 2;
- how most languages would add 1 and 2
(+ 1 2)
- how lisp would add 1 and 2
The reason lisp uses prefix notation is due to the second big take away you need
to get your head around when you learn lisp. In lisp everything is an expression
and in the case of our above example +
is a function call and 1
and 2
are
our inputs into the +
function. Due to the fact that everything is in an
expression in lisp it also important to know why this matters. Everything is an
expression and everything will return a result.
Variables in lisp can be named with many different characters. Some of these
include: letters, numbers, +
, -
, _
, *
, =
, <
, >
, ?
, !
. It is
important to note though that we can not use whitespace in variable names.