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

Introduction to Lisp (dpc13)

Introduction to Lisp (dpc13)

A4b95be2145cc46f891707b6db9dd82d?s=128

Igor Wiedler

June 07, 2013
Tweet

Transcript

  1. Introduction to

  2. @igorwesome

  3. (

  4. John McCarthy

  5. None
  6. f o r g e t everything y o u

    k n o w
  7. (+ 1 2)

  8. (+ 1 2) l i s t

  9. (+ 1 2) symbol

  10. (+ 1 2) number

  11. (+ 1 2) number

  12. (+ 1 2) a p p l i c a

    t i o n
  13. (+ 1 2) function

  14. (+ 1 2) arguments

  15. 3

  16. None
  17. cons

  18. car

  19. first

  20. cdr

  21. rest

  22. (a . b)

  23. a b

  24. (a . nil)

  25. a

  26. (a . (b . (c . nil)))

  27. a b c

  28. (a b c)

  29. [:a :b :c]

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

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

  32. a b c

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

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

  35. a b c

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

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

  38. a b c

  39. (+ 2 3 5 7 11 13)

  40. (/ (+ (- b) (math.sqrt (- (math.expt b 2) (*

    4 a c)))) (* 2 a)) (/ (- (- b) (math.sqrt (- (math.expt b 2) (* 4 a c)))) (* 2 a))
  41. 5 3.14 "fubar" variable :keyword

  42. (fn [x] x)

  43. (fn [x] x) arguments

  44. (fn [x] x) body

  45. (def answer 42)

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

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

  48. (hello "dpc13")

  49. (defn member? [x coll] (cond (empty? coll) false :else (or

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

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

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

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

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

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

    (= (first coll) x) (member? x (rest coll)))))
  56. (member? :c [:a :b :c :d])

  57. (defn member? [x coll] (cond (empty? coll) false :else (or

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

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

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

    (= (first coll) x) (member? x (rest coll))))) (member? :c [:a :b :c :d])
  61. (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])
  62. (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])
  63. (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])
  64. (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])
  65. (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])
  66. (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])
  67. (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])
  68. (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
  69. (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
  70. (defn member? [x coll] (cond (empty? coll) false :else (or

    (= (first coll) x) (member? x (rest coll))))) (member? :c [:a :b :c :d]) => true
  71. true

  72. Collections

  73. Filter

  74. 0 1 2 3 4 even?

  75. 0 1 2 3 4 even? 0

  76. 0 1 2 3 4 even? 0

  77. 0 1 2 3 4 even? 0 2

  78. 0 1 2 3 4 even? 0 2

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

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

  81. Map

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

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

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

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

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

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

    9 6 8
  88. (map #(+ 5 %) [0 1 2 3 4])

  89. Reduce

  90. 3 6 7 9 + 0 running total

  91. 3 6 7 9 + 0 3 running total

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

  93. 3 6 7 9 + 0 9 3 16 running

    total
  94. 3 6 7 9 + 0 9 25 3 16

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

    running total
  96. (reduce + 0 [3 6 7 9])

  97. 0 1 2 3 4 even? 0 2 4 #(+

    5 %) 5 7 9 + 5 12 21
  98. Macros

  99. (foo bar baz)

  100. "(+ 1 2)" Reader + 1 2 + 1 2

    Eval 3 Compile-time Runtime x 0 y 1 z 2
  101. "(a 1 2)" Reader a 1 2 Compile-time Macro b

    5 6 a 1 2
  102. (defn id [x] x) => (def id (clojure.core/fn ([x] x)))

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

    (first (reverse [:foo :bar :baz])))) =>
  104. (defmacro unless [pred a b] `(if (not ~pred) ~a ~b))

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

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

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

    whoops }
  108. class Foo ($bar) { } => class Foo { private

    $bar; function __construct($bar) { $this->bar = $bar; } }
  109. ($x): $x => fn ($x) { return $x; } =>

    function ($x) { return $x; }
  110. Y-Combinator

  111. (defn Y [le] ((fn [f] (f f)) (fn [f] (le

    #((f f) %)))))
  112. (defn fac [f] (fn [x] (if (zero? x) 1 (*

    x (f (dec x))))))
  113. ((Y fac) 5)

  114. ((Y ) 5) (fn [f] (fn [x] (if (zero? x)

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

    1 (* x (f (dec x)))))) (fn [le] ((fn [f] (f f)) (fn [f] (le #((f f) %)))))
  116. Lambda Calculus

  117. 0: (fn [f] identity) 1: (fn [f] f) 2: (fn

    [f] #(f (f %))) 3: (fn [f] #(f (f (f %))))
  118. ((n inc) 0)

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

  120. ( 0) identity

  121. 0

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

  123. ( 0) inc

  124. 1

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

  126. ( 0) #(inc (inc %))

  127. 2

  128. (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)))
  129. Meta-circular evaluator

  130. • = • fn • cond • cons • first

    • rest • quote
  131. (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)))))
  132. 62 LOC

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

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

  135. )

  136. None
  137. None
  138. None
  139. • www.lisperati.com/logo.html • codon.com/programming-with-nothing • fogus.me/static/preso/magnificent7/

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