Pro Yearly is on sale from $80 to $50! »

Lisp (laraconeu)

Lisp (laraconeu)

A4b95be2145cc46f891707b6db9dd82d?s=128

Igor Wiedler

August 30, 2014
Tweet

Transcript

  1. None
  2. None
  3. Computers

  4. What are they even?

  5. @igorwhiletrue

  6. A little history

  7. brb inventing programming

  8. computers they’re so great <3 Alonzo Church

  9. None
  10. John McCarthy

  11. I <3 computers but hate assembly language

  12. None
  13. None
  14. (

  15. None
  16. ( ) parentheses are hugs

  17. ❤ ( )

  18. How to read lisp code, in one easy step

  19. foo(1, 2)

  20. (foo 1 2)

  21. (+ 1 2)

  22. 3

  23. Growing a language

  24. a bar foo atom

  25. None
  26. cons

  27. first

  28. rest

  29. a b

  30. (a . b)

  31. a

  32. (a . nil)

  33. a b c

  34. (a . (b . (c . nil)))

  35. (a b c)

  36. (foo (bar) (baz qux))

  37. foo bar baz qux

  38. foo(bar(), baz(qux))

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

    fn arg 0 call call call
  40. (foo (bar) (baz qux))

  41. (foo (bar) (baz qux)) fn arg 0 arg 1

  42. (foo (bar) (baz qux)) fn arg 0 fn

  43. (quote (a b c))

  44. '(a b c)

  45. (first '(a b c)) !

  46. (first '(a b c)) ! 'a

  47. a b c

  48. (rest '(a b c)) !

  49. (rest '(a b c)) ! '(b c)

  50. a b c

  51. (cons 'a '(b c)) !

  52. (cons 'a '(b c)) ! '(a b c)

  53. a b c

  54. 5 3.14 "fubar" variable :keyword

  55. (fn [x] x)

  56. (fn [x] x) arguments

  57. (fn [x] x) body

  58. (def answer 42)

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

  60. (hello “Laracon”)

  61. No loops

  62. Recursive functions

  63. in order to understand rec ursion, one must first understand

    re cursion,
  64. (defn member? [x coll] (cond (empty? coll) false :else (or

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

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

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

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

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

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

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

    (= (first coll) x) (member? x (rest coll)))))
  72. (member? 'c '(a b c d))

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

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

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

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

    (= (first coll) x) (member? x (rest coll))))) ! (member? 'c '(a b c d))
  77. (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))
  78. (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))
  79. (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))
  80. (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))
  81. (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))
  82. (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))
  83. (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))
  84. (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
  85. (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
  86. (defn member? [x coll] (cond (empty? coll) false :else (or

    (= (first coll) x) (member? x (rest coll))))) ! (member? 'c '(a b c d)) => true
  87. true

  88. Macros

  89. (foo (bar) (baz qux))

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

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

    5 6 a 1 2
  92. (defn id [x] x) ! (def id (fn [x] x))

    =>
  93. (-> '(foo bar baz) reverse first name string/reverse) (string/reverse (name

    (first (reverse '(foo bar baz)))) =>
  94. class Foo ($bar) { } class Foo { private $bar;

    function __construct($bar) { $this->bar = $bar; } } =>
  95. Backporting?

  96. function () use ($foo) { return qux($foo); } => ()

    ==> qux($foo)
  97. hhvm 5.6 5.5 5.4 FI ...

  98. Computer Science

  99. Computer Science

  100. Computer Science

  101. Y-Combinator

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

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

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

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

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

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

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

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

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

  111. ( 0) identity

  112. 0

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

  114. ( 0) inc

  115. 1

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

  117. ( 0) #(inc (inc %))

  118. 2

  119. (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)))
  120. Meta-circular evaluator

  121. (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)))))
  122. 62 LOC

  123. 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]; }
  124. 68 LOC

  125. PHPPHP: 6208 LOC github.com/ircmaxell/PHPPHP

  126. PHPPHP: 6208 LOC github.com/ircmaxell/PHPPHP (parser +6169 LOC)

  127. )

  128. None
  129. None
  130. None
  131. None
  132. Questions? • joind.in/11697 • github.com/igorw • lambda-php • lusp •

    ilias • galapagos • @igorwhiletrue
  133. None
  134. 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/
  135. • 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/