Gentle Introduction To Lisp
-
Upload
damien-garaud -
Category
Presentations & Public Speaking
-
view
536 -
download
0
Transcript of Gentle Introduction To Lisp
(GENTLE) INTRODUCTION TO LISPFunctional Programming Meetup
Bordeaux – Node2016-03-30
Damien Garaud / @jazzydag
WHO AM I?Damien Garaud
Scientist Programmer
Trainer & learning-addict
@jazzydag
https://github.com/garaud
A LITTLE STORYDimitri Fontaine aka @tapoueh
a PostgreSQL expert and Lisper-friendly
loads data into PostgreSQL fastpgloader
"I switched from Python to Common Lispbecause I wanted to use a modern
language" - @tapoueh
7th European Lisp Symposium
More than 20x faster than the previous Python version [1]
LISP?
( )
TINY DEMOIt's gonna be legen... wait for it
M‐x irony‐mode
HISTORY1958 John McCarthy MITLISP: LISt ProcessingNot "Lots of Irritating and Silly Parentheses"1970: Lisp MachinesArtificial Intelligence1980: need for standardization (CommonLisp)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 (2008BEAM)
Hylang (2013 Lisp & Python)
LANGUAGES FEATURESDynamic TypingFunctional & ImperativeHigh-order FunctionsObject-orientedPrefix (polish) NotationMacrosGarbage CollectorSource as a datastructure
... of course, it's just a list
S-EXPRESSIONList () or an atom
Each element is separated by awhitespace
An element can be:
a listan atom
ATOMSBasic Type
stringintegerfloatnil
A symbol
variablenamefunctionname
EXAMPLES(1 "two" 5 "jazz" 4.2)
(helloworld "John")
(fibo 12)
(/ (+ a b) (* c d))
(foo (bar "baz") "quz")
FUNCTION DEFINITIONdefun 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 oneboolean
EVALUATION: HOW DOES IT WORKJust read the S-expressionValid S-expressionValid Lisp expressionSuppose the first element is a function or anoperatorLeft to the rightEvaluate 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)
REPLRead
Eval
Loop
RETURNED VALUEThe last S-expression
(if (zerop 0) (+ 5 3) (* 2 2))
Which value?
8
"IF" AND THE STANDARDEVALUATION RULE
(if (zerop 0) (+ 1 3) (* 3 2))
Can you see the problem?
Special operators
Not really evaluated asusual
NOT ALWAYS EVALUATIONYou saw the if. What's else?
(+ 2 5)
(let ((a 2) (b 21)) (* a b))
Variables evaluation: it's OKWhat about the high-order functionsYou want to pass a name of afunctionNot evaluate me please
HIGHER-ORDER FUNCTIONS(defun algo (pred struct) "docstring" (impl))
(algo pred (list "coltrane" "davis" "hancock"))
pred shouldn't be evaluated asisvariable name errorSpecial "quote"
SPECIAL QUOTEThere 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)
TAKE A BREATHWhat 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
MACROSIt's about code generation
... but not really like C macros
The programmable programming language
MACROS BY EXAMPLEMake a list as Python does:
seq = [] for elt in range(10): if elt % 2 == 0: seq.append(elt)
# use comprehension listseq = [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 condtest) "doc here" (body))
Lisp code generation writing Lisp code
FURTHERCLOS: Common Lisp ObjectSystem
Quicklisp: a package manager
Compilation: SBCL or Clozure CL
Make a Lisp
REFERENCESSICPThe Little SchemerPractical CommonLispCommon Lisp Recipes