gentle introduction to lisp

27
(GENTLE) INTRODUCTION TO LISP Functional Programming Meetup Bordeaux – Node 2016-03-30 Damien Garaud / @jazzydag

Upload: damien-garaud

Post on 15-Jan-2017

542 views

Category:

Presentations & Public Speaking


0 download

TRANSCRIPT

Page 1: Gentle Introduction To Lisp

(GENTLE) INTRODUCTION TO LISPFunctional Programming Meetup

Bordeaux – Node2016-03-30

Damien Garaud / @jazzydag

Page 2: Gentle Introduction To Lisp

WHO AM I?Damien Garaud

Scientist Programmer

Trainer & learning-addict

@jazzydag

https://github.com/garaud

Page 3: Gentle Introduction To Lisp

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]

Page 4: Gentle Introduction To Lisp

LISP?

( )

Page 5: Gentle Introduction To Lisp

TINY DEMOIt's gonna be legen... wait for it

M‐x irony‐mode

Page 6: Gentle Introduction To Lisp

HISTORY1958 John McCarthy MITLISP: LISt ProcessingNot "Lots of Irritating and Silly Parentheses"1970: Lisp MachinesArtificial Intelligence1980: need for standardization (CommonLisp)1994: ANSI Common Lisp

Page 7: Gentle Introduction To 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)

Page 8: Gentle Introduction To Lisp

LANGUAGES FEATURESDynamic TypingFunctional & ImperativeHigh-order FunctionsObject-orientedPrefix (polish) NotationMacrosGarbage CollectorSource as a datastructure

... of course, it's just a list

Page 9: Gentle Introduction To Lisp

S-EXPRESSIONList () or an atom

Each element is separated by awhitespace

An element can be:

a listan atom

Page 10: Gentle Introduction To Lisp

ATOMSBasic Type

stringintegerfloatnil

A symbol

variablenamefunctionname

Page 11: Gentle Introduction To Lisp

EXAMPLES(1 "two" ­5 "jazz" 4.2) 

(hello­world "John") 

(fibo 12) 

(/ (+ a b) (* c d)) 

(foo (bar "baz") "quz") 

Page 12: Gentle Introduction To Lisp

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

Page 13: Gentle Introduction To Lisp

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

Page 14: Gentle Introduction To Lisp

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)

Page 15: Gentle Introduction To Lisp

REPLRead

Eval

Print

Loop

Page 16: Gentle Introduction To Lisp

RETURNED VALUEThe last S-expression

(if (zerop 0)   (+ 5 3)   (* 2 2)) 

Which value?

8

Page 17: Gentle Introduction To Lisp

"IF" AND THE STANDARDEVALUATION RULE

(if (zerop 0)   (+ 1 3)   (* 3 2)) 

Can you see the problem?

Special operators

Not really evaluated asusual

Page 18: Gentle Introduction To Lisp

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

Page 19: Gentle Introduction To Lisp

HIGHER-ORDER FUNCTIONS(defun algo (pred struct)   "docstring"   (impl)) 

(algo pred (list "coltrane" "davis" "hancock")) 

pred shouldn't be evaluated asisvariable name errorSpecial "quote"

Page 20: Gentle Introduction To Lisp

SPECIAL QUOTEThere 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) 

Page 21: Gentle Introduction To Lisp

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

Page 22: Gentle Introduction To Lisp

MACROSIt's about code generation

... but not really like C macros

The programmable programming language

Page 23: Gentle Introduction To Lisp

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

Page 24: Gentle Introduction To 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

Page 25: Gentle Introduction To Lisp

FURTHERCLOS: Common Lisp ObjectSystem

Quicklisp: a package manager

Compilation: SBCL or Clozure CL

Make a Lisp

Page 27: Gentle Introduction To Lisp

THANKS

https://xkcd.com/224/