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. (

  2. 3

  3. car

  4. cdr

  5. a b

  6. a

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

    4 a c)))) (* 2 a)) (/ (- (- b) (math.sqrt (- (math.expt b 2) (* 4 a c)))) (* 2 a))
  8. (defn member? [x coll] (cond (empty? coll) false :else (or

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

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

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

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

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

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

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

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

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

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

    (= (first coll) x) (member? x (rest coll))))) (member? :c [:a :b :c :d])
  19. (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])
  20. (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])
  21. (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])
  22. (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])
  23. (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])
  24. (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])
  25. (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])
  26. (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
  27. (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
  28. (defn member? [x coll] (cond (empty? coll) false :else (or

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

  30. 3 6 7 9 + 0 9 25 3 16

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

    running total
  32. 0 1 2 3 4 even? 0 2 4 #(+

    5 %) 5 7 9 + 5 12 21
  33. "(+ 1 2)" Reader + 1 2 + 1 2

    Eval 3 Compile-time Runtime x 0 y 1 z 2
  34. class Foo ($bar) { } => class Foo { private

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

    function ($x) { return $x; }
  36. (( ) 5) (fn [f] (fn [x] (if (zero? x)

    1 (* x (f (dec x)))))) (fn [le] ((fn [f] (f f)) (fn [f] (le #((f f) %)))))
  37. 0: (fn [f] identity) 1: (fn [f] f) 2: (fn

    [f] #(f (f %))) 3: (fn [f] #(f (f (f %))))
  38. 0

  39. 1

  40. 2

  41. (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)))
  42. (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)))))
  43. )