This document provides an introduction to the Lisp programming language. It discusses the history of Lisp, which was created in 1958. It also covers key Lisp concepts like S-expressions, atoms, function definition, evaluation, and macros. Macros allow programmers to generate Lisp code from Lisp code, extending the language. The document uses examples to demonstrate Lisp evaluation and features like conditional evaluation, higher-order functions, and special forms like 'quote and 'if.
2. WHO AM I?
Damien Garaud
Scientist Programmer
Trainer & learning-addict
@jazzydag
https://github.com/garaud
3. A LITTLE STORY
Dimitri Fontaine aka @tapoueh
a PostgreSQL expert and Lisper-friendly
loads data into PostgreSQL fastpgloader
"I switched from Python to Common Lisp
because I wanted to use a modern
language" - @tapoueh
7th European Lisp Symposium
More than 20x faster than the previous Python version [1]
6. HISTORY
1958 John McCarthy MIT
LISP: LISt Processing
Not "Lots of Irritating and Silly Parentheses"
1970: Lisp Machines
Artificial Intelligence
1980: need for standardization (Common
Lisp)
1994: ANSI Common Lisp
7. WHICH LISP?
Common Lisp
Scheme (1970)
Emacs Lisp (1976)
Racket (1996)
Arc (2001 Paul Graham)
Clojure (2007 JVM)
LFE as Lisp Flavored Erlang (2008
BEAM)
Hylang (2013 Lisp & Python)
8. LANGUAGES FEATURES
Dynamic Typing
Functional & Imperative
High-order Functions
Object-oriented
Prefix (polish) Notation
Macros
Garbage Collector
Source as a data
structure
... of course, it's just a list
9. S-EXPRESSION
List () or an atom
Each element is separated by a
whitespace
An element can be:
a list
an atom
13. EVALUATION: HOW DOES IT WORK
Just read the S-expression
Valid S-expression
Valid Lisp expression
Suppose the first element is a function or an
operator
Left to the right
Evaluate all but first, then apply the first
17. "IF" AND THE STANDARD
EVALUATION RULE
(if (zerop 0)
(+ 1 3)
(* 3 2))
Can you see the problem?
Special operators
Not really evaluated as
usual
18. NOT ALWAYS EVALUATION
You saw the if. What's else?
(+ 2 5)
(let ((a 2)
(b 21))
(* a b))
Variables evaluation: it's OK
What about the high-order functions
You want to pass a name of a
function
Not evaluate me please
20. SPECIAL QUOTE
There is a function for that:
(quote (a b c d))
(defun mypred (lhs rhs)
"my predicate func"
(> lhs rhs))
(algo (quote mypred) struct)
special ' syntax
(algo 'mypred struct)
21. TAKE A BREATH
What did we see:
Evaluation rules
S-expressions, S-expressions everywhere!
Source code as a data structure
(defun foo (arg) (body)) is a S-
expression
Thus arg can be a simple list or...
... a function definition
Don't worry... the end is coming
22. MACROS
It's about code generation
... but not really like C macros
The programmable programming language
23. MACROS BY EXAMPLE
Make a list as Python does:
seq = []
for elt in range(10):
if elt % 2 == 0:
seq.append(elt)
# use comprehension list
seq = [x for x in range(10) if x % 2 == 0]
Comprehension Lists
And you want to implement this feature in Common Lisp