(GENTLE) INTRODUCTION TO LISP
Functional Programming Meetup
Bordeaux – Node
2016-03-30
Damien Garaud / @jazzydag
WHO AM I?
Damien Garaud
Scientist Programmer
Trainer & learning-addict
@jazzydag
https://github.com/garaud
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]
LISP?
( )
TINY DEMO
It's gonna be legen... wait for it
M‐x irony‐mode
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
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)
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
S-EXPRESSION
List () or an atom
Each element is separated by a
whitespace
An element can be:
a list
an atom
ATOMS
Basic Type
string
integer
float
nil
A symbol
variable
name
function
name
EXAMPLES
(1 "two" ­5 "jazz" 4.2) 
(hello­world "John") 
(fibo 12) 
(/ (+ a b) (* c d)) 
(foo (bar "baz") "quz") 
FUNCTION DEFINITION
defun keyword
(defun hello (name) 
  "print hello" 
  (format t "Hello ~a!~%" name)) 
(hello "lambda meetup") 
This is a S-expression
with two nested S-exprs, 4 symbols, two strings and one
boolean
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
EXAMPLES
(+ (* 2 15) (* 6 2)) 
42
(hello "you")
"Hello you!"
(let ((name "lambda meetup")) 
  (hello name)) 
"Hello lambda meetup!"
(mapcar 'evenp (list 0 1 2 3 4)) 
(T nil T nil T)
REPL
Read
Eval
Print
Loop
RETURNED VALUE
The last S-expression
(if (zerop 0) 
  (+ 5 3) 
  (* 2 2)) 
Which value?
8
"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
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
HIGHER-ORDER FUNCTIONS
(defun algo (pred struct) 
  "docstring" 
  (impl)) 
(algo pred (list "coltrane" "davis" "hancock")) 
pred shouldn't be evaluated as
is
variable name error
Special "quote"
SPECIAL QUOTE
There is a function for that:
(quote (a b c d)) 
(defun my­pred (lhs rhs) 
  "my predicate func" 
  (> lhs rhs)) 
(algo (quote my­pred) struct) 
special ' syntax
(algo 'my­pred struct) 
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
MACROS
It's about code generation
... but not really like C macros
The programmable programming language
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
MACROS BY EXAMPLE
(lcomp x for x in (range 10) if (= (mod x 2) 0)) 
You can define your own language extension
(defmacro lcomp (expression for var in list cond cond­test) 
  "doc here" 
  (body)) 
Lisp code generation writing Lisp code
FURTHER
CLOS: Common Lisp Object
System
Quicklisp: a package manager
Compilation: SBCL or Clozure CL
Make a Lisp
REFERENCES
SICP
The Little Schemer
Practical Common
Lisp
Common Lisp Recipes
THANKS
https://xkcd.com/224/

Gentle Introduction To Lisp