Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Introduction to Lisp (dpc13)

Introduction to Lisp (dpc13)

Igor Wiedler

June 07, 2013
Tweet

More Decks by Igor Wiedler

Other Decks in Programming

Transcript

  1. Introduction to

    View full-size slide

  2. John McCarthy

    View full-size slide

  3. f o r g e t
    everything
    y o u k n o w

    View full-size slide

  4. (+ 1 2)
    l i s t

    View full-size slide

  5. (+ 1 2)
    symbol

    View full-size slide

  6. (+ 1 2)
    number

    View full-size slide

  7. (+ 1 2)
    number

    View full-size slide

  8. (+ 1 2)
    a p p l i c a t i o n

    View full-size slide

  9. (+ 1 2)
    function

    View full-size slide

  10. (+ 1 2)
    arguments

    View full-size slide

  11. (a . (b . (c . nil)))

    View full-size slide

  12. (first [:a :b :c])

    View full-size slide

  13. (first [:a :b :c])
    :a

    View full-size slide

  14. (rest [:a :b :c])

    View full-size slide

  15. (rest [:a :b :c])
    [:b :c]

    View full-size slide

  16. (cons :a [:b :c])

    View full-size slide

  17. (cons :a [:b :c])
    [:a :b :c]

    View full-size slide

  18. (+ 2 3 5 7 11 13)

    View full-size slide

  19. (/ (+ (- b)
    (math.sqrt (- (math.expt b 2)
    (* 4 a c))))
    (* 2 a))
    (/ (- (- b)
    (math.sqrt (- (math.expt b 2)
    (* 4 a c))))
    (* 2 a))

    View full-size slide

  20. 5
    3.14
    "fubar"
    variable
    :keyword

    View full-size slide

  21. (fn [x] x)
    arguments

    View full-size slide

  22. (fn [x] x)
    body

    View full-size slide

  23. (def answer 42)

    View full-size slide

  24. (def hello
    (fn [name]
    (str "Hello " name)))

    View full-size slide

  25. (defn hello [name]
    (str "Hello " name))

    View full-size slide

  26. (hello "dpc13")

    View full-size slide

  27. (defn member? [x coll]
    (cond
    (empty? coll) false
    :else (or (= (first coll) x)
    (member? x (rest coll)))))

    View full-size slide

  28. (defn member? [x coll]
    (cond
    (empty? coll) false
    :else (or (= (first coll) x)
    (member? x (rest coll)))))

    View full-size slide

  29. (defn member? [x coll]
    (cond
    (empty? coll) false
    :else (or (= (first coll) x)
    (member? x (rest coll)))))

    View full-size slide

  30. (defn member? [x coll]
    (cond
    (empty? coll) false
    :else (or (= (first coll) x)
    (member? x (rest coll)))))

    View full-size slide

  31. (defn member? [x coll]
    (cond
    (empty? coll) false
    :else (or (= (first coll) x)
    (member? x (rest coll)))))

    View full-size slide

  32. (defn member? [x coll]
    (cond
    (empty? coll) false
    :else (or (= (first coll) x)
    (member? x (rest coll)))))

    View full-size slide

  33. (defn member? [x coll]
    (cond
    (empty? coll) false
    :else (or (= (first coll) x)
    (member? x (rest coll)))))

    View full-size slide

  34. (member? :c [:a :b :c :d])

    View full-size slide

  35. (defn member? [x coll]
    (cond
    (empty? coll) false
    :else (or (= (first coll) x)
    (member? x (rest coll)))))
    (member? :c [:a :b :c :d])

    View full-size slide

  36. (defn member? [x coll]
    (cond
    (empty? coll) false
    :else (or (= (first coll) x)
    (member? x (rest coll)))))
    (member? :c [:a :b :c :d])

    View full-size slide

  37. (defn member? [x coll]
    (cond
    (empty? coll) false
    :else (or (= (first coll) x)
    (member? x (rest coll)))))
    (member? :c [:a :b :c :d])

    View full-size slide

  38. (defn member? [x coll]
    (cond
    (empty? coll) false
    :else (or (= (first coll) x)
    (member? x (rest coll)))))
    (member? :c [:a :b :c :d])

    View full-size slide

  39. (defn member? [x coll]
    (cond
    (empty? coll) false
    :else (or (= (first coll) x)
    (member? x (rest coll)))))
    (member? :c [:a :b :c :d])
    (member? :c [:b :c :d])

    View full-size slide

  40. (defn member? [x coll]
    (cond
    (empty? coll) false
    :else (or (= (first coll) x)
    (member? x (rest coll)))))
    (member? :c [:a :b :c :d])
    (member? :c [:b :c :d])

    View full-size slide

  41. (defn member? [x coll]
    (cond
    (empty? coll) false
    :else (or (= (first coll) x)
    (member? x (rest coll)))))
    (member? :c [:a :b :c :d])
    (member? :c [:b :c :d])

    View full-size slide

  42. (defn member? [x coll]
    (cond
    (empty? coll) false
    :else (or (= (first coll) x)
    (member? x (rest coll)))))
    (member? :c [:a :b :c :d])
    (member? :c [:b :c :d])

    View full-size slide

  43. (defn member? [x coll]
    (cond
    (empty? coll) false
    :else (or (= (first coll) x)
    (member? x (rest coll)))))
    (member? :c [:a :b :c :d])
    (member? :c [:b :c :d])
    (member? :c [:c :d])

    View full-size slide

  44. (defn member? [x coll]
    (cond
    (empty? coll) false
    :else (or (= (first coll) x)
    (member? x (rest coll)))))
    (member? :c [:a :b :c :d])
    (member? :c [:b :c :d])
    (member? :c [:c :d])

    View full-size slide

  45. (defn member? [x coll]
    (cond
    (empty? coll) false
    :else (or (= (first coll) x)
    (member? x (rest coll)))))
    (member? :c [:a :b :c :d])
    (member? :c [:b :c :d])
    (member? :c [:c :d])

    View full-size slide

  46. (defn member? [x coll]
    (cond
    (empty? coll) false
    :else (or (= (first coll) x)
    (member? x (rest coll)))))
    (member? :c [:a :b :c :d])
    (member? :c [:b :c :d])
    (member? :c [:c :d]) => true

    View full-size slide

  47. (defn member? [x coll]
    (cond
    (empty? coll) false
    :else (or (= (first coll) x)
    (member? x (rest coll)))))
    (member? :c [:a :b :c :d])
    (member? :c [:b :c :d]) => true

    View full-size slide

  48. (defn member? [x coll]
    (cond
    (empty? coll) false
    :else (or (= (first coll) x)
    (member? x (rest coll)))))
    (member? :c [:a :b :c :d]) => true

    View full-size slide

  49. 0
    1
    2
    3
    4
    even?
    0
    2

    View full-size slide

  50. 0
    1
    2
    3
    4
    even?
    0
    2

    View full-size slide

  51. 0
    1
    2
    3
    4
    even?
    0
    2
    4

    View full-size slide

  52. (filter even? [0 1 2 3 4])

    View full-size slide

  53. 0
    1
    2
    3
    4
    #(+ 5 %)

    View full-size slide

  54. 0
    1
    2
    3
    4
    #(+ 5 %)
    5

    View full-size slide

  55. 0
    1
    2
    3
    4
    #(+ 5 %)
    5
    6

    View full-size slide

  56. 0
    1
    2
    3
    4
    #(+ 5 %)
    5
    7
    6

    View full-size slide

  57. 0
    1
    2
    3
    4
    #(+ 5 %)
    5
    7
    6
    8

    View full-size slide

  58. 0
    1
    2
    3
    4
    #(+ 5 %)
    5
    7
    9
    6
    8

    View full-size slide

  59. (map #(+ 5 %) [0 1 2 3 4])

    View full-size slide

  60. 3
    6
    7
    9
    +
    0
    running total

    View full-size slide

  61. 3
    6
    7
    9
    +
    0
    3
    running total

    View full-size slide

  62. 3
    6
    7
    9
    +
    0
    9
    3
    running total

    View full-size slide

  63. 3
    6
    7
    9
    +
    0
    9
    3
    16
    running total

    View full-size slide

  64. 3
    6
    7
    9
    +
    0
    9
    25
    3
    16
    running total

    View full-size slide

  65. 3
    6
    7
    9
    +
    0
    9
    25
    3
    16
    running total

    View full-size slide

  66. (reduce + 0 [3 6 7 9])

    View full-size slide

  67. 0
    1
    2
    3
    4
    even?
    0
    2
    4
    #(+ 5 %)
    5
    7
    9
    +
    5
    12
    21

    View full-size slide

  68. (foo bar baz)

    View full-size slide

  69. "(+ 1 2)" Reader
    +
    1 2
    +
    1 2
    Eval 3
    Compile-time
    Runtime
    x 0
    y 1
    z 2

    View full-size slide

  70. "(a 1 2)" Reader
    a
    1 2
    Compile-time
    Macro
    b
    5 6
    a
    1 2

    View full-size slide

  71. (defn id [x] x)
    =>
    (def id (clojure.core/fn ([x] x)))

    View full-size slide

  72. (-> [:foo :bar :baz]
    reverse
    first
    name
    string/reverse)
    (string/reverse
    (name
    (first
    (reverse [:foo :bar :baz]))))
    =>

    View full-size slide

  73. (defmacro unless [pred a b]
    `(if (not ~pred) ~a ~b))

    View full-size slide

  74. (unless foo x y)
    =>
    (if (not foo) x y)

    View full-size slide

  75. unless ($foo)
    =>
    if (!$foo)

    View full-size slide

  76. retry (5) {
    failing_call();
    } catch ($e) {
    // ... whoops
    }

    View full-size slide

  77. class Foo ($bar) {
    }
    =>
    class Foo {
    private $bar;
    function __construct($bar) {
    $this->bar = $bar;
    }
    }

    View full-size slide

  78. ($x): $x
    =>
    fn ($x) { return $x; }
    =>
    function ($x) { return $x; }

    View full-size slide

  79. Y-Combinator

    View full-size slide

  80. (defn Y [le]
    ((fn [f] (f f))
    (fn [f]
    (le #((f f) %)))))

    View full-size slide

  81. (defn fac [f]
    (fn [x]
    (if (zero? x)
    1
    (* x (f (dec x))))))

    View full-size slide

  82. ((Y ) 5)
    (fn [f]
    (fn [x]
    (if (zero? x)
    1
    (* x (f (dec x))))))

    View full-size slide

  83. (( ) 5)
    (fn [f]
    (fn [x]
    (if (zero? x)
    1
    (* x (f (dec x))))))
    (fn [le]
    ((fn [f] (f f))
    (fn [f]
    (le #((f f) %)))))

    View full-size slide

  84. Lambda Calculus

    View full-size slide

  85. 0: (fn [f] identity)
    1: (fn [f] f)
    2: (fn [f] #(f (f %)))
    3: (fn [f] #(f (f (f %))))

    View full-size slide

  86. (( inc) 0)
    (fn [f] identity)

    View full-size slide

  87. ( 0)
    identity

    View full-size slide

  88. (( inc) 0)
    (fn [f] f)

    View full-size slide

  89. (( inc) 0)
    (fn [f] #(f (f %)))

    View full-size slide

  90. ( 0)
    #(inc (inc %))

    View full-size slide

  91. (defn INC [n]
    (fn [p]
    (fn [x]
    (p ((n p) x)))))
    (defn DEC [n]
    (fn [f]
    (fn [x]
    (((n (fn [g] (fn [h] (h (g f)))))
    (fn [y] x))
    (fn [y] y)))))
    (defn ADD [m]
    (fn [n]
    ((n INC) m)))
    (defn MUL [m]
    (fn [n]
    ((n (ADD m)) ZERO)))

    View full-size slide

  92. Meta-circular
    evaluator

    View full-size slide

  93. • =
    • fn
    • cond
    • cons
    • first
    • rest
    • quote

    View full-size slide

  94. (defun null. (x)
    (eq x '()))
    (defun and. (x y)
    (cond (x (cond (y 't) ('t '())))
    ('t '())))
    (defun not. (x)
    (cond (x '())
    ('t 't)))
    (defun append. (x y)
    (cond ((null. x) y)
    ('t (cons (car x) (append. (cdr x) y)))))
    (defun list. (x y)
    (cons x (cons y '())))
    (defun pair. (x y)
    (cond ((and. (null. x) (null. y)) '())
    ((and. (not. (atom x)) (not. (atom y)))
    (cons (list. (car x) (car y))
    (pair. (cdr x) (cdr y))))))
    (defun assoc. (x y)
    (cond ((eq (caar y) x) (cadar y))
    ('t (assoc. x (cdr y)))))
    (defun eval. (e a)
    (cond
    ((atom e) (assoc. e a))
    ((atom (car e))
    (cond
    ((eq (car e) 'quote) (cadr e))
    ((eq (car e) 'atom) (atom (eval. (cadr e) a)))
    ((eq (car e) 'eq) (eq (eval. (cadr e) a)
    (eval. (caddr e) a)))
    ((eq (car e) 'car) (car (eval. (cadr e) a)))
    ((eq (car e) 'cdr) (cdr (eval. (cadr e) a)))
    ((eq (car e) 'cons) (cons (eval. (cadr e) a)
    (eval. (caddr e) a)))
    ((eq (car e) 'cond) (evcon. (cdr e) a))
    ('t (eval. (cons (assoc. (car e) a)
    (cdr e))
    a))))
    ((eq (caar e) 'label)
    (eval. (cons (caddar e) (cdr e))
    (cons (list. (cadar e) (car e)) a)))
    ((eq (caar e) 'lambda)
    (eval. (caddar e)
    (append. (pair. (cadar e) (evlis. (cdr e) a))
    a)))))
    (defun evcon. (c a)
    (cond ((eval. (caar c) a)
    (eval. (cadar c) a))
    ('t (evcon. (cdr c) a))))
    (defun evlis. (m a)
    (cond ((null. m) '())
    ('t (cons (eval. (car m) a)
    (evlis. (cdr m) a)))))

    View full-size slide

  95. PHPPHP: 6208 LOC
    github.com/ircmaxell/PHPPHP
    After lunch, E102

    View full-size slide

  96. PHPPHP: 6208 LOC
    github.com/ircmaxell/PHPPHP
    After lunch, E102
    (parser +6169 LOC)

    View full-size slide

  97. • www.lisperati.com/logo.html
    • codon.com/programming-with-nothing
    • fogus.me/static/preso/magnificent7/

    View full-size slide

  98. Questions?
    • joind.in/8442
    • clojure.org
    • @igorwesome

    View full-size slide