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

Lisp (laraconeu)

Lisp (laraconeu)

Igor Wiedler

August 30, 2014
Tweet

More Decks by Igor Wiedler

Other Decks in Programming

Transcript

  1. (

  2. 3

  3. a b

  4. a

  5. foo bar baz qux fn arg 0 arg 1 fn

    fn arg 0 call call call
  6. (defn member? [x coll] (cond (empty? coll) false :else (or

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

    (= (first coll) x) (member? x (rest coll)))))
  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))))) ! (member? 'c '(a b c d))
  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)) (member? 'c '(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)) (member? 'c '(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)) => true
  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)) => 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)) => true
  28. "(+ 1 2)" Reader + 1 2 + 1 2

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

    function __construct($bar) { $this->bar = $bar; } } =>
  30. (( ) 5) (fn [f] (fn [x] (if (zero? x)

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

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

  33. 1

  34. 2

  35. (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)))
  36. (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)))))
  37. function evaluate($expr, $env = []) { if (is_string($expr)) { if

    (is_numeric($expr)) { $val = (float) $expr; return [$val, $env]; } if ('true' === $expr) { return [true, $env]; } if ('false' === $expr) { return [false, $env]; } if ('null' === $expr) { return [null, $env]; } $var = $expr; return [$env[$var], $env]; } $fn = array_shift($expr); $args = $expr; if ('define' === $fn) { list($var, $val) = $args; list($val, $env) = evaluate($val, $env); $env = array_merge($env, [$var => $val]); return [null, $env]; } if ('lambda' === $fn) { list($lambda_args, $code) = $args; $closure = ['closure', $lambda_args, $code, $env]; return [$closure, $env]; } if ('if' === $fn) { list($cond, $then, $else) = $args; $cond = evaluate($cond, $env)[0]; if ($cond === true) { $val = evaluate($then, $env)[0]; } else { $val = evaluate($else, $env)[0]; } return [$val, $env]; } ! $fn = evaluate($fn, $env)[0]; $args = array_map(function ($arg) use ($env) { return evaluate($arg, $env)[0]; }, $args); return apply($fn, $args, $env); } ! function apply($fn, $args, $env) { list($_, $lambda_args, $code, $closure_env) = $fn; ! $env = array_merge($env, $closure_env); $env = array_merge($env, array_combine($lambda_args, $args)); return evaluate_code($code, $env); } function evaluate_code($code, $env = []) { foreach ($code as $expr) { list($val, $env) = evaluate($expr, $env); } return [$val, $env]; }
  38. )

  39. References • Lisp Logo by Conrad Barski
 lisperati.com/logo.html • Jesus

    bleibet meine Freude by Jeffrey Campbell • Programming with Nothing by Tom Stuart
 codon.com/programming-with-nothing • The Magnificent Seven by Michael Fogus
 fogus.me/static/preso/magnificent7/
  40. • Recursive functions of symbolic expressions and their computation by

    machine, Part I by John McCarthy
 www-formal.stanford.edu/jmc/recursive.pdf • Growing a Language by Guy Steele
 youtube.com/watch?v=_ahvzDzKdB0 • The Lambda Papers by Guy Steele and Gerald Sussman
 library.readscheme.org/page1.html • SICP lectures by Hal Abelson and Gerald Sussman
 groups.csail.mit.edu/mac/classes/6.001/abelson- sussman-lectures/