SlideShare a Scribd company logo
1 of 43
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

More Related Content

Recently uploaded

Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024
Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024
Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024
Victor Rentea
 
Architecting Cloud Native Applications
Architecting Cloud Native ApplicationsArchitecting Cloud Native Applications
Architecting Cloud Native Applications
WSO2
 
Finding Java's Hidden Performance Traps @ DevoxxUK 2024
Finding Java's Hidden Performance Traps @ DevoxxUK 2024Finding Java's Hidden Performance Traps @ DevoxxUK 2024
Finding Java's Hidden Performance Traps @ DevoxxUK 2024
Victor Rentea
 

Recently uploaded (20)

Artificial Intelligence Chap.5 : Uncertainty
Artificial Intelligence Chap.5 : UncertaintyArtificial Intelligence Chap.5 : Uncertainty
Artificial Intelligence Chap.5 : Uncertainty
 
Apidays New York 2024 - The value of a flexible API Management solution for O...
Apidays New York 2024 - The value of a flexible API Management solution for O...Apidays New York 2024 - The value of a flexible API Management solution for O...
Apidays New York 2024 - The value of a flexible API Management solution for O...
 
Platformless Horizons for Digital Adaptability
Platformless Horizons for Digital AdaptabilityPlatformless Horizons for Digital Adaptability
Platformless Horizons for Digital Adaptability
 
Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024
Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024
Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024
 
ICT role in 21st century education and its challenges
ICT role in 21st century education and its challengesICT role in 21st century education and its challenges
ICT role in 21st century education and its challenges
 
Boost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdfBoost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdf
 
Apidays New York 2024 - Passkeys: Developing APIs to enable passwordless auth...
Apidays New York 2024 - Passkeys: Developing APIs to enable passwordless auth...Apidays New York 2024 - Passkeys: Developing APIs to enable passwordless auth...
Apidays New York 2024 - Passkeys: Developing APIs to enable passwordless auth...
 
Six Myths about Ontologies: The Basics of Formal Ontology
Six Myths about Ontologies: The Basics of Formal OntologySix Myths about Ontologies: The Basics of Formal Ontology
Six Myths about Ontologies: The Basics of Formal Ontology
 
Polkadot JAM Slides - Token2049 - By Dr. Gavin Wood
Polkadot JAM Slides - Token2049 - By Dr. Gavin WoodPolkadot JAM Slides - Token2049 - By Dr. Gavin Wood
Polkadot JAM Slides - Token2049 - By Dr. Gavin Wood
 
Exploring Multimodal Embeddings with Milvus
Exploring Multimodal Embeddings with MilvusExploring Multimodal Embeddings with Milvus
Exploring Multimodal Embeddings with Milvus
 
CNIC Information System with Pakdata Cf In Pakistan
CNIC Information System with Pakdata Cf In PakistanCNIC Information System with Pakdata Cf In Pakistan
CNIC Information System with Pakdata Cf In Pakistan
 
Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...
Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...
Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...
 
Introduction to Multilingual Retrieval Augmented Generation (RAG)
Introduction to Multilingual Retrieval Augmented Generation (RAG)Introduction to Multilingual Retrieval Augmented Generation (RAG)
Introduction to Multilingual Retrieval Augmented Generation (RAG)
 
MS Copilot expands with MS Graph connectors
MS Copilot expands with MS Graph connectorsMS Copilot expands with MS Graph connectors
MS Copilot expands with MS Graph connectors
 
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost SavingRepurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
 
WSO2's API Vision: Unifying Control, Empowering Developers
WSO2's API Vision: Unifying Control, Empowering DevelopersWSO2's API Vision: Unifying Control, Empowering Developers
WSO2's API Vision: Unifying Control, Empowering Developers
 
Architecting Cloud Native Applications
Architecting Cloud Native ApplicationsArchitecting Cloud Native Applications
Architecting Cloud Native Applications
 
MINDCTI Revenue Release Quarter One 2024
MINDCTI Revenue Release Quarter One 2024MINDCTI Revenue Release Quarter One 2024
MINDCTI Revenue Release Quarter One 2024
 
Strategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a FresherStrategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a Fresher
 
Finding Java's Hidden Performance Traps @ DevoxxUK 2024
Finding Java's Hidden Performance Traps @ DevoxxUK 2024Finding Java's Hidden Performance Traps @ DevoxxUK 2024
Finding Java's Hidden Performance Traps @ DevoxxUK 2024
 

Featured

How Race, Age and Gender Shape Attitudes Towards Mental Health
How Race, Age and Gender Shape Attitudes Towards Mental HealthHow Race, Age and Gender Shape Attitudes Towards Mental Health
How Race, Age and Gender Shape Attitudes Towards Mental Health
ThinkNow
 
Social Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie InsightsSocial Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie Insights
Kurio // The Social Media Age(ncy)
 

Featured (20)

2024 State of Marketing Report – by Hubspot
2024 State of Marketing Report – by Hubspot2024 State of Marketing Report – by Hubspot
2024 State of Marketing Report – by Hubspot
 
Everything You Need To Know About ChatGPT
Everything You Need To Know About ChatGPTEverything You Need To Know About ChatGPT
Everything You Need To Know About ChatGPT
 
Product Design Trends in 2024 | Teenage Engineerings
Product Design Trends in 2024 | Teenage EngineeringsProduct Design Trends in 2024 | Teenage Engineerings
Product Design Trends in 2024 | Teenage Engineerings
 
How Race, Age and Gender Shape Attitudes Towards Mental Health
How Race, Age and Gender Shape Attitudes Towards Mental HealthHow Race, Age and Gender Shape Attitudes Towards Mental Health
How Race, Age and Gender Shape Attitudes Towards Mental Health
 
AI Trends in Creative Operations 2024 by Artwork Flow.pdf
AI Trends in Creative Operations 2024 by Artwork Flow.pdfAI Trends in Creative Operations 2024 by Artwork Flow.pdf
AI Trends in Creative Operations 2024 by Artwork Flow.pdf
 
Skeleton Culture Code
Skeleton Culture CodeSkeleton Culture Code
Skeleton Culture Code
 
PEPSICO Presentation to CAGNY Conference Feb 2024
PEPSICO Presentation to CAGNY Conference Feb 2024PEPSICO Presentation to CAGNY Conference Feb 2024
PEPSICO Presentation to CAGNY Conference Feb 2024
 
Content Methodology: A Best Practices Report (Webinar)
Content Methodology: A Best Practices Report (Webinar)Content Methodology: A Best Practices Report (Webinar)
Content Methodology: A Best Practices Report (Webinar)
 
How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024
 
Social Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie InsightsSocial Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie Insights
 
Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024
 
5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summary5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summary
 
ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd
 
Getting into the tech field. what next
Getting into the tech field. what next Getting into the tech field. what next
Getting into the tech field. what next
 
Google's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search IntentGoogle's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search Intent
 
How to have difficult conversations
How to have difficult conversations How to have difficult conversations
How to have difficult conversations
 
Introduction to Data Science
Introduction to Data ScienceIntroduction to Data Science
Introduction to Data Science
 
Time Management & Productivity - Best Practices
Time Management & Productivity -  Best PracticesTime Management & Productivity -  Best Practices
Time Management & Productivity - Best Practices
 
The six step guide to practical project management
The six step guide to practical project managementThe six step guide to practical project management
The six step guide to practical project management
 
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
 

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