lisp (vs ruby) metaprogramming

9,119 views

Published on

a very basic introduction to lisp macros in comparison to ruby meta-programming

Published in: Technology
3 Comments
23 Likes
Statistics
Notes
No Downloads
Views
Total views
9,119
On SlideShare
0
From Embeds
0
Number of Embeds
73
Actions
Shares
0
Downloads
0
Comments
3
Likes
23
Embeds 0
No embeds

No notes for slide

lisp (vs ruby) metaprogramming

  1. 1. Lisp Metaprogramming The programmable programming language Friday, February 19, 2010
  2. 2. I’d rather play COD Friday, February 19, 2010
  3. 3. “So, Ruby was a Lisp originally, in theory. Let's call it MatzLisp from now on. ;-)” -Matz Friday, February 19, 2010
  4. 4. "Lisp isn't a language, it's a building material." "the greatest single programming language ever designed" “OOP to me means only messaging, local retention and protection and hiding of state- process, and extreme late-binding of all things. It can be done in Smalltalk and in Lisp. There are possibly other systems in which this is possible, but I'm not aware of them.” -Alan Kay Friday, February 19, 2010
  5. 5. Friday, February 19, 2010
  6. 6. As seen in Lisp since 1958 late binding garbage collection dynamic typing if-then-else construct function type recursion symbol type Interactive development Lisp Machines ... Friday, February 19, 2010
  7. 7. As seen in Lisp since 1958 late binding garbage collection dynamic typing Scheme if-then-else construct Common Lisp function type Clojure recursion Arc symbol type NewLISP Interactive development (JavaScript) Lisp Machines ... Friday, February 19, 2010
  8. 8. Lisp in 6 slides Friday, February 19, 2010
  9. 9. Data structure: Linked lists Friday, February 19, 2010
  10. 10. Data structure: Linked lists ((1 2) (3 4) (5 6)) Friday, February 19, 2010
  11. 11. Syntax: Linked lists (again) (define y-combinator (lambda (f) ((lambda (x) (f (x x))) (lambda (x) (f (x x)))))) (homoiconicity) Friday, February 19, 2010
  12. 12. Semantics: λ - Calculus λxy.x Friday, February 19, 2010
  13. 13. Semantics: λ - Calculus λxy.x (lambda (x y) x) Friday, February 19, 2010
  14. 14. Semantics: λ - Calculus λxy.x (lambda (x y) x) (λxy.x 2 4) -β> 2 Friday, February 19, 2010
  15. 15. Semantics: λ - Calculus λxy.x (lambda (x y) x) (λxy.x 2 4) -β> 2 ((lambda (x y) x) 2 4) -> 2 Friday, February 19, 2010
  16. 16. Semantics: evaluation rule Read-Eval-Print-Loop (+ 3 (- 2 1)) (+ 3 1) 4 Friday, February 19, 2010
  17. 17. Semantics: special forms backquote/unquote (backquote (+ 2 3)) -> (+ 2 3) `(+ 2 3) -> (+ 2 3) Friday, February 19, 2010
  18. 18. Semantics: special forms backquote/unquote (backquote (+ 2 3)) -> (+ 2 3) `(+ 2 3) -> (+ 2 3) `(unquote (+ 2 3)) -> 5 `,(+ 2 3) -> 5 Friday, February 19, 2010
  19. 19. (Common) Lisp Macros Friday, February 19, 2010
  20. 20. Why macros? “Pascal is for building pyramids -- imposing, breathtaking, static structures built by armies pushing heavy blocks into place. Lisp is for building organisms -- imposing, breathtaking, dynamic structures built by squads fitting fluctuating myriads of simpler organisms into place. [...] Invent and fit; have fits and reinvent!” -from SICP foreword Friday, February 19, 2010
  21. 21. Russian dolls Lisp user forms special forms kernel language Friday, February 19, 2010
  22. 22. Russian dolls language extensions Lisp user forms special forms kernel language Friday, February 19, 2010
  23. 23. Functions vs Macros Function S-Expressions (f x) Values Macro S-Expressions (f x) S-Expressions Friday, February 19, 2010
  24. 24. Functions vs Macros S-Expressions Macro expansion (f x) time S-Expressions’ Compile / execution (f’ x) time Values Friday, February 19, 2010
  25. 25. The my-unless function (defun my-unless (c ef et) (if c et ef)) Friday, February 19, 2010
  26. 26. The my-unless function (defun my-unless (c ef et) (if c et ef)) (my-unless t 1 2) -> 2 (my-unless nil 1 2) -> 1 Friday, February 19, 2010
  27. 27. The my-unless function (defun my-unless (c ef et) (if c et ef)) (my-unless t 1 2) -> 2 (my-unless nil 1 2) -> 1 (my-unless t (print “test”) 1) ->”test” 1 Friday, February 19, 2010
  28. 28. The my-unless macro (defmacro my-unless (c ef et) (if c et ef)) Friday, February 19, 2010
  29. 29. The my-unless macro (defmacro my-unless (c ef et) (if c et ef)) (my-unless t (print “test”) 1) ->1 Friday, February 19, 2010
  30. 30. The my-unless macro (defmacro my-unless (c ef et) (if c et ef)) (my-unless t (print “test”) 1) ->1 (define *c* t) (my-unless *c* (print “test”) 1) Friday, February 19, 2010
  31. 31. The my-unless macro (defmacro my-unless (c ef et) `(if ,c ,et ,ef)) (define *c* t) (my-unless *c* (print “test”) 1) Friday, February 19, 2010
  32. 32. The my-unless macro (defmacro my-unless (c ef et) `(if ,c ,et ,ef)) (define *c* t) (my-unless *c* (print “test”) 1) (define *c* t) (if *c* 1 (print “test”)) Friday, February 19, 2010
  33. 33. The my-unless macro (defmacro my-unless (c ef et) `(if ,c ,et ,ef)) (define *c* t) (my-unless *c* (print “test”) 1) (define *c* t) 1 (if *c* 1 (print “test”)) Friday, February 19, 2010
  34. 34. Macros taxonomy flow modification “with” macros -> abstracting pattens (with-file) (with- gearman-request) Object Oriented Lisp (CLOS) and Meta Object Protocol Compilers, pasers, etc. Functional lisp: monads, comonads, Tarski arrows, currying, lazy evaluation Friday, February 19, 2010
  35. 35. The Macro Club The first rule of the Macro Club is Don’t Write Macros The second rule of Macro Club is Write Macros If That Is The Only Way to Encapsulate a Pattern -from Programming Clojure Friday, February 19, 2010
  36. 36. Ruby metaprogramming? Friday, February 19, 2010
  37. 37. Ruby metaprogramming is broken and cannot be fixed (imho) Friday, February 19, 2010
  38. 38. (Lisp) [Ruby] Simple regular sintax Complex sintax Complex undefined Simple defined semantics semantics Code = lists Code = strings Code manipulation = list Code manipulation = string manipulation funs. manipulation funs. Macro expansion time Eval at run time Friday, February 19, 2010
  39. 39. Alternatives: RubyAST, Ruby parser Code = AST objects Code Manipulation = Objects Manipulation Friday, February 19, 2010
  40. 40. No magic please Friday, February 19, 2010
  41. 41. References Friday, February 19, 2010
  42. 42. The Seasoned On Lisp, Schemer, Paul Graham Friedman Programming SICP, Clojure, Abelson et alt. Stuart Halloway Friday, February 19, 2010
  43. 43. λ Friday, February 19, 2010

×