SlideShare a Scribd company logo
Lisp Metaprogramming
          The programmable programming language




Friday, February 19, 2010
I’d rather play COD




Friday, February 19, 2010
“So, Ruby was a Lisp originally, in theory.
                            Let's call it MatzLisp from now on. ;-)”




                                                                  -Matz
Friday, February 19, 2010
"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
Friday, February 19, 2010
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
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
Lisp in 6 slides




Friday, February 19, 2010
Data structure: Linked lists




Friday, February 19, 2010
Data structure: Linked lists



                            ((1 2) (3 4) (5 6))


Friday, February 19, 2010
Syntax: Linked lists (again)

                            (define y-combinator
                              (lambda (f)
                                ((lambda (x) (f (x x)))
                                 (lambda (x) (f (x x))))))

                                    (homoiconicity)
Friday, February 19, 2010
Semantics: λ - Calculus

                            λxy.x




Friday, February 19, 2010
Semantics: λ - Calculus

                                λxy.x
                            (lambda (x y) x)




Friday, February 19, 2010
Semantics: λ - Calculus

                                 λxy.x
                             (lambda (x y) x)

                            (λxy.x 2 4) -β> 2


Friday, February 19, 2010
Semantics: λ - Calculus

                                 λxy.x
                             (lambda (x y) x)

                            (λxy.x 2 4) -β> 2
                       ((lambda (x y) x) 2 4) -> 2
Friday, February 19, 2010
Semantics: evaluation rule

                            Read-Eval-Print-Loop

                               (+ 3 (- 2 1))
                                  (+ 3 1)
                                     4
Friday, February 19, 2010
Semantics: special forms

                 backquote/unquote
                (backquote (+ 2 3)) -> (+ 2 3)
                     `(+ 2 3) -> (+ 2 3)



Friday, February 19, 2010
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
(Common) Lisp Macros




Friday, February 19, 2010
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
Russian dolls



                                Lisp
                            user forms
                            special forms
                            kernel language




Friday, February 19, 2010
Russian dolls


                            language extensions
                                Lisp
                            user forms
                            special forms
                            kernel language




Friday, February 19, 2010
Functions vs Macros

                            Function

           S-Expressions      (f x)    Values


                             Macro

           S-Expressions      (f x)    S-Expressions

Friday, February 19, 2010
Functions vs Macros
                                        S-Expressions
                              Macro
                            expansion        (f x)
                               time
                                        S-Expressions’
                            Compile /
                            execution        (f’ x)
                              time
                                           Values
Friday, February 19, 2010
The my-unless function
                            (defun my-unless (c ef et)
                               (if c et ef))




Friday, February 19, 2010
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
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
The my-unless macro
                            (defmacro my-unless (c ef et)
                               (if c et ef))




Friday, February 19, 2010
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
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
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
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
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
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
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
Ruby metaprogramming?




Friday, February 19, 2010
Ruby metaprogramming is
          broken and cannot be fixed
                            (imho)




Friday, February 19, 2010
(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
Alternatives: RubyAST, Ruby
          parser


                                 Code = AST objects
                     Code Manipulation = Objects Manipulation




Friday, February 19, 2010
No magic please




Friday, February 19, 2010
References




Friday, February 19, 2010
The Seasoned
                            On Lisp,
                                              Schemer,
                            Paul Graham
                                              Friedman




                                              Programming
                            SICP,
                                              Clojure,
                            Abelson et alt.
                                              Stuart Halloway

Friday, February 19, 2010
λ

Friday, February 19, 2010
Ad

Recommended

PDF
Practical NLP with Lisp
Vsevolod Dyomkin
 
PDF
Metaprogramming and Reflection in Common Lisp
Damien Cassou
 
KEY
LISP: How I Learned To Stop Worrying And Love Parantheses
Dominic Graefen
 
PPTX
Artificial Intelligence in Virus Detection & Recognition
ahmadali999
 
PPTX
From Narrow AI to Artificial General Intelligence (AGI)
Helgi Páll Helgason, PhD
 
PDF
Scaling Twitter
Blaine
 
PDF
2008-01-25 Tangible Value
Lin Jen-Shin
 
PDF
2010 05-20-clojure concurrency--jugd
Kwanzoo Dev
 
PPTX
A brief introduction to lisp language
David Gu
 
PPTX
LISP: Introduction to lisp
DataminingTools Inc
 
PPTX
LISP: Introduction To Lisp
LISP Content
 
PPT
INTRODUCTION TO LISP
Nilt1234
 
PDF
Of Rats And Dragons
Sean Cribbs
 
PPTX
Lisp
sonukumar142
 
PDF
Erlang Introduction
Serhiy Oplakanets
 
PDF
Type systems
Daniel Quirino Oliveira
 
PDF
Boost your-oop-with-fp
Uberto Barbini
 
PDF
Lisp for Python Programmers
Vsevolod Dyomkin
 
PDF
Introduction To Lisp
kyleburton
 
ZIP
Lisp Primer Key
Yuumi Yoshida
 
PDF
Javascript the Language of the Web
andersjanmyr
 
PPTX
Report about the LISP Programming Language
maldosmelandrew
 
PPT
Lisp and scheme i
Luis Goldster
 
KEY
Five Languages in a Moment
Sergio Gil
 
PDF
Developing a Language
evanphx
 
PPTX
Lisp
huzaifa ramzan
 
PDF
Patterns
David Nolen
 
PDF
Developing a Language
Engine Yard
 
PDF
API Modeling Framework: a toolbox ofr API specs. Gluecon 2017
Antonio Garrote Hernández
 
PDF
Linked Data APIs (Funding Circle May 2015)
Antonio Garrote Hernández
 

More Related Content

Similar to lisp (vs ruby) metaprogramming (20)

PPTX
A brief introduction to lisp language
David Gu
 
PPTX
LISP: Introduction to lisp
DataminingTools Inc
 
PPTX
LISP: Introduction To Lisp
LISP Content
 
PPT
INTRODUCTION TO LISP
Nilt1234
 
PDF
Of Rats And Dragons
Sean Cribbs
 
PPTX
Lisp
sonukumar142
 
PDF
Erlang Introduction
Serhiy Oplakanets
 
PDF
Type systems
Daniel Quirino Oliveira
 
PDF
Boost your-oop-with-fp
Uberto Barbini
 
PDF
Lisp for Python Programmers
Vsevolod Dyomkin
 
PDF
Introduction To Lisp
kyleburton
 
ZIP
Lisp Primer Key
Yuumi Yoshida
 
PDF
Javascript the Language of the Web
andersjanmyr
 
PPTX
Report about the LISP Programming Language
maldosmelandrew
 
PPT
Lisp and scheme i
Luis Goldster
 
KEY
Five Languages in a Moment
Sergio Gil
 
PDF
Developing a Language
evanphx
 
PPTX
Lisp
huzaifa ramzan
 
PDF
Patterns
David Nolen
 
PDF
Developing a Language
Engine Yard
 
A brief introduction to lisp language
David Gu
 
LISP: Introduction to lisp
DataminingTools Inc
 
LISP: Introduction To Lisp
LISP Content
 
INTRODUCTION TO LISP
Nilt1234
 
Of Rats And Dragons
Sean Cribbs
 
Erlang Introduction
Serhiy Oplakanets
 
Boost your-oop-with-fp
Uberto Barbini
 
Lisp for Python Programmers
Vsevolod Dyomkin
 
Introduction To Lisp
kyleburton
 
Lisp Primer Key
Yuumi Yoshida
 
Javascript the Language of the Web
andersjanmyr
 
Report about the LISP Programming Language
maldosmelandrew
 
Lisp and scheme i
Luis Goldster
 
Five Languages in a Moment
Sergio Gil
 
Developing a Language
evanphx
 
Patterns
David Nolen
 
Developing a Language
Engine Yard
 

More from Antonio Garrote Hernández (7)

PDF
API Modeling Framework: a toolbox ofr API specs. Gluecon 2017
Antonio Garrote Hernández
 
PDF
Linked Data APIs (Funding Circle May 2015)
Antonio Garrote Hernández
 
PDF
Message Passing Concurrency in Clojure using Kilim
Antonio Garrote Hernández
 
PDF
4th European Lisp Symposium: Jobim: an Actors Library for the Clojure Program...
Antonio Garrote Hernández
 
PDF
RESTful writable APIs for the web of Linked Data using relational storage sol...
Antonio Garrote Hernández
 
PDF
Developing Distributed Semantic Systems
Antonio Garrote Hernández
 
PDF
Egearmand: an Erlang Gearman daemon
Antonio Garrote Hernández
 
API Modeling Framework: a toolbox ofr API specs. Gluecon 2017
Antonio Garrote Hernández
 
Linked Data APIs (Funding Circle May 2015)
Antonio Garrote Hernández
 
Message Passing Concurrency in Clojure using Kilim
Antonio Garrote Hernández
 
4th European Lisp Symposium: Jobim: an Actors Library for the Clojure Program...
Antonio Garrote Hernández
 
RESTful writable APIs for the web of Linked Data using relational storage sol...
Antonio Garrote Hernández
 
Developing Distributed Semantic Systems
Antonio Garrote Hernández
 
Egearmand: an Erlang Gearman daemon
Antonio Garrote Hernández
 
Ad

Recently uploaded (20)

PDF
PyCon SG 25 - Firecracker Made Easy with Python.pdf
Muhammad Yuga Nugraha
 
PDF
“MPU+: A Transformative Solution for Next-Gen AI at the Edge,” a Presentation...
Edge AI and Vision Alliance
 
PPTX
Wenn alles versagt - IBM Tape schützt, was zählt! Und besonders mit dem neust...
Josef Weingand
 
PDF
EIS-Webinar-Engineering-Retail-Infrastructure-06-16-2025.pdf
Earley Information Science
 
PDF
Securing AI - There Is No Try, Only Do!.pdf
Priyanka Aash
 
PDF
9-1-1 Addressing: End-to-End Automation Using FME
Safe Software
 
PDF
Hyderabad MuleSoft In-Person Meetup (June 21, 2025) Slides
Ravi Tamada
 
PDF
AI Agents and FME: A How-to Guide on Generating Synthetic Metadata
Safe Software
 
PDF
Cracking the Code - Unveiling Synergies Between Open Source Security and AI.pdf
Priyanka Aash
 
PPTX
Security Tips for Enterprise Azure Solutions
Michele Leroux Bustamante
 
PDF
Using the SQLExecutor for Data Quality Management: aka One man's love for the...
Safe Software
 
PDF
Database Benchmarking for Performance Masterclass: Session 2 - Data Modeling ...
ScyllaDB
 
PDF
Techniques for Automatic Device Identification and Network Assignment.pdf
Priyanka Aash
 
PPTX
"How to survive Black Friday: preparing e-commerce for a peak season", Yurii ...
Fwdays
 
PDF
The Future of Product Management in AI ERA.pdf
Alyona Owens
 
PDF
AI vs Human Writing: Can You Tell the Difference?
Shashi Sathyanarayana, Ph.D
 
PDF
Coordinated Disclosure for ML - What's Different and What's the Same.pdf
Priyanka Aash
 
PDF
Agentic AI for Developers and Data Scientists Build an AI Agent in 10 Lines o...
All Things Open
 
PPTX
Curietech AI in action - Accelerate MuleSoft development
shyamraj55
 
PDF
Salesforce Summer '25 Release Frenchgathering.pptx.pdf
yosra Saidani
 
PyCon SG 25 - Firecracker Made Easy with Python.pdf
Muhammad Yuga Nugraha
 
“MPU+: A Transformative Solution for Next-Gen AI at the Edge,” a Presentation...
Edge AI and Vision Alliance
 
Wenn alles versagt - IBM Tape schützt, was zählt! Und besonders mit dem neust...
Josef Weingand
 
EIS-Webinar-Engineering-Retail-Infrastructure-06-16-2025.pdf
Earley Information Science
 
Securing AI - There Is No Try, Only Do!.pdf
Priyanka Aash
 
9-1-1 Addressing: End-to-End Automation Using FME
Safe Software
 
Hyderabad MuleSoft In-Person Meetup (June 21, 2025) Slides
Ravi Tamada
 
AI Agents and FME: A How-to Guide on Generating Synthetic Metadata
Safe Software
 
Cracking the Code - Unveiling Synergies Between Open Source Security and AI.pdf
Priyanka Aash
 
Security Tips for Enterprise Azure Solutions
Michele Leroux Bustamante
 
Using the SQLExecutor for Data Quality Management: aka One man's love for the...
Safe Software
 
Database Benchmarking for Performance Masterclass: Session 2 - Data Modeling ...
ScyllaDB
 
Techniques for Automatic Device Identification and Network Assignment.pdf
Priyanka Aash
 
"How to survive Black Friday: preparing e-commerce for a peak season", Yurii ...
Fwdays
 
The Future of Product Management in AI ERA.pdf
Alyona Owens
 
AI vs Human Writing: Can You Tell the Difference?
Shashi Sathyanarayana, Ph.D
 
Coordinated Disclosure for ML - What's Different and What's the Same.pdf
Priyanka Aash
 
Agentic AI for Developers and Data Scientists Build an AI Agent in 10 Lines o...
All Things Open
 
Curietech AI in action - Accelerate MuleSoft development
shyamraj55
 
Salesforce Summer '25 Release Frenchgathering.pptx.pdf
yosra Saidani
 
Ad

lisp (vs ruby) metaprogramming

  • 1. Lisp Metaprogramming The programmable programming language Friday, February 19, 2010
  • 2. I’d rather play COD Friday, February 19, 2010
  • 3. “So, Ruby was a Lisp originally, in theory. Let's call it MatzLisp from now on. ;-)” -Matz Friday, February 19, 2010
  • 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
  • 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. 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. Lisp in 6 slides Friday, February 19, 2010
  • 9. Data structure: Linked lists Friday, February 19, 2010
  • 10. Data structure: Linked lists ((1 2) (3 4) (5 6)) Friday, February 19, 2010
  • 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. Semantics: λ - Calculus λxy.x Friday, February 19, 2010
  • 13. Semantics: λ - Calculus λxy.x (lambda (x y) x) Friday, February 19, 2010
  • 14. Semantics: λ - Calculus λxy.x (lambda (x y) x) (λxy.x 2 4) -β> 2 Friday, February 19, 2010
  • 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. Semantics: evaluation rule Read-Eval-Print-Loop (+ 3 (- 2 1)) (+ 3 1) 4 Friday, February 19, 2010
  • 17. Semantics: special forms backquote/unquote (backquote (+ 2 3)) -> (+ 2 3) `(+ 2 3) -> (+ 2 3) Friday, February 19, 2010
  • 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. (Common) Lisp Macros Friday, February 19, 2010
  • 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. Russian dolls Lisp user forms special forms kernel language Friday, February 19, 2010
  • 22. Russian dolls language extensions Lisp user forms special forms kernel language Friday, February 19, 2010
  • 23. Functions vs Macros Function S-Expressions (f x) Values Macro S-Expressions (f x) S-Expressions Friday, February 19, 2010
  • 24. Functions vs Macros S-Expressions Macro expansion (f x) time S-Expressions’ Compile / execution (f’ x) time Values Friday, February 19, 2010
  • 25. The my-unless function (defun my-unless (c ef et) (if c et ef)) Friday, February 19, 2010
  • 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. 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. The my-unless macro (defmacro my-unless (c ef et) (if c et ef)) Friday, February 19, 2010
  • 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. 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. 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. 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. 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. 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. 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
  • 37. Ruby metaprogramming is broken and cannot be fixed (imho) Friday, February 19, 2010
  • 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. Alternatives: RubyAST, Ruby parser Code = AST objects Code Manipulation = Objects Manipulation Friday, February 19, 2010
  • 40. No magic please Friday, February 19, 2010
  • 42. The Seasoned On Lisp, Schemer, Paul Graham Friedman Programming SICP, Clojure, Abelson et alt. Stuart Halloway Friday, February 19, 2010