SlideShare a Scribd company logo
Macros in Lisp
CL macro facility allows the user to define arbitrary functions that convert certain lisp forms into different forms before evaluating or compiling them.Macros makes it possible to write codes that are clear and elegant.In CL, macros are not functionsIn particular, macros cannot be used as the functional arguments to functions such as apply, funcall, or map.
overviewMacro definitionMacro expansionDestructuringCompiler macrosEnvironments
Macro definitiondefmacro construct is used to define a macro.Macro-function determines whether the given function is the name of the macro.macro-function symbolThe argument must be a symbol, if the function has a global function definition( macro definition) then the expansion function is returned else nil is returned.Macro-function cannot work on local variables it can only determine the global definitions.Setf is used as a macro function to install a macro as a symbols global function definitionTo add an optional environment argument to macro-function:macro-function symbol &optional env(setf (macro-function symbol)  fn)
defmacro has the same syntax as the defunHere, name is the symbol whose macro definition we are creating.Lambda-list is similar in form to a lambda-list, Forms constitute the body of the expander function.The defmacro function returns the name.defmacro name lambda-list [[ {declaration}* | doc-string ]]  {form}* [Macro]
Defmacro lambda-list keywordsSome of the lambda-list key words are: &optional, &rest, &key, &allow-other-keys, &aux&body:  this is identical in function to &rest, it informs certain output-formatting and editing functions that the remainder of the form is treated as a body and should be intended accordingly.&whole: this is followed by a single variable that is bound to the entire macro-call form.&environment: this is followed by a single variable that is bound to an environment representing the lexical environment in which the macro call is to be interpreted.
Macro-expansionIf form is a macro call, then macroexpand-1 will expand the macro call once and return two values.( the expansion and t) if form is not macro call then two values ( form and nil) are returned.A form is said to be a macro call only if it is a cons whose car is a symbol that names a macro.The macro expansion function is used for expanding a macro call.
Macro-expansion steps followedOnce the macroexpand-1 has determined that a symbol names a macro, it obtains the expansion function for that macro.The value of the variable *macro-expand-hook* is then called as a function of three arguments: the expansion function, the form, and the environment env.The value returned from this call is taken to be the expansion of the macro call.The evaluator makes use of the macro calls as if through the use of macroexpand-1; eval also makes use of *macroexpand-hook*Macroexpand is similar to macroexpand-1, but repeatedly expands forms until it is no longer a macro call.
DestructingTo make the destructing feature of the defmacro available use:This macro binds the variables specified in lambda-list to the corresponding values in the tree structure resulting from evaluating the expression, then executes the forms as the an implicit progn.If the result of the evaluating expression does not match the destructuring pattern, an error should be signaled.Destructuring-bind lambda-list expression {declaration}* {form}[Macro]
Compiler macrosCompiler macros are used to transform the selective source code when the compiler is processing the code.This is similar to defmacro except the definition is not stored in the symbol function cell of name and is not seen by macroexpand-1The lambda-list may include &environment, &whole and may include destructuring.Return-form is used to exit from the function.define-compiler-macro name lambda-list                         {declaration | doc-string}* {form}*
Name must be a symbol, if it has been defined as a compiler macro, then the compiler-macro-function returns the macro expansion function, else returns nil.Expander function is obtained as if by a call to compiler-macro-function on the car of the form rather than by a call to macro-function.Two values are returned, the expansion and the value if expansion is true else nil.compiler macro function name &optional envcompiler-macroexpand from &optional  envcompiler-macroexpand-1 from &optional  env
Three cases when no expansion occurs:There is no compiler macro definition for the car of the form.There is such a definition but there is also a not inline declaration, either globally or in lexical environment env.A global compiler macro definition is shadowed by a local function or macro definition.
EnvironmentsThere is a minimal set of accessors and a constructor for environments.Define-declaration is used to define all the standard declaration specifiers, with the exception of special.The function returns the interpretation  of the symbol variable when it appears as a variable within the lexical environment env.Three values are returned:The first value indicates the type of definition in the env which may be :special, :lexical, :symbol-macro, :constantVariable-information variable &optional env
2. The second value is true if there is a local binding else it returns nil.3.  The third value is an a-list containing information about the declarations that apply to the apparent binding of the variable.Returns the information about the  interpretation of the function when it appears in the functional position of the lexical environment env.This function also returns three values:Type of definition or binding of the function name which is apparent in env (nil, :function, :macro, :special-form) The second value is true if the function is local else nil.The third value is an a-list containing information about declarations that apply to the apparent binding of the function.function-information function  &optional env
argument-environment env &key :variable  :symbol-macro :fuction :macro :declarereturns a new environment containing the information present in enc augmented with the information provided by the key word arguments.The arguments used are::variable list of symbols that will be visible as bound variable in the new environment,.
:symbol-macro  list of symbol macro definitions
:function  list of function-names that will be visible as local function bindings in the new environment.
:macro list of local macro definitions
:declare list of declaration specifiersdefine-declaration decl-name lambda-list {form}*The macro defines a macro for the named declaration.The function defined by this macro will be called with two augments, a declaration specifier whose car is decl-name and the env argument to augment-environment.

More Related Content

PPTX
System software - macro expansion,nested macro calls
PDF
Power of call symput data
PPT
PDF
Unit 2
PDF
Chapter 13.1.6
PPTX
plsql tutorialhub....
PDF
Assignment#08
PDF
33443223 system-software-unit-iv
System software - macro expansion,nested macro calls
Power of call symput data
Unit 2
Chapter 13.1.6
plsql tutorialhub....
Assignment#08
33443223 system-software-unit-iv

What's hot (20)

PDF
Assignment#01
DOCX
Oracle plsql and d2 k interview question1
DOCX
Oracle plsql and d2 k interview questions
PPTX
PPTX
Functions
DOC
4. function
PPTX
BAS 150 Lesson 8 Lecture
DOC
Programming style guideline very good
PPT
ELENA MICROPROCESSOR
PPT
SAS Macros part 3
PPTX
functions
DOC
Complete list of all sap abap keywords
PPT
Lecture11 abap on line
PPTX
Ism normalization pine valley 2012
PPT
SAS Macros part 2
PPTX
Procedure and Functions in pl/sql
PPT
DAC training-batch -2020(PLSQL)
PPT
SQL / PL
PPT
6. Integrity and Security in DBMS
PPTX
Modularisation techniques new
Assignment#01
Oracle plsql and d2 k interview question1
Oracle plsql and d2 k interview questions
Functions
4. function
BAS 150 Lesson 8 Lecture
Programming style guideline very good
ELENA MICROPROCESSOR
SAS Macros part 3
functions
Complete list of all sap abap keywords
Lecture11 abap on line
Ism normalization pine valley 2012
SAS Macros part 2
Procedure and Functions in pl/sql
DAC training-batch -2020(PLSQL)
SQL / PL
6. Integrity and Security in DBMS
Modularisation techniques new
Ad

Viewers also liked (20)

PPTX
Retrieving Data From A Database
PPTX
Introduction to Data-Applied
PPTX
Data Applied: Association
PPTX
Communicating simply
PPTX
RapidMiner: Nested Subprocesses
PPTX
Procedures And Functions in Matlab
PPTX
R: Apply Functions
PPTX
R Environment
PPT
PresentacióN De Quimica
PPTX
XL-MINER:Prediction
PPTX
Data Applied:Tree Maps
PPT
Paramount Search Partners
PPTX
MySql:Basics
DOC
建築師法修正草案總說明
PPTX
MED dra Coding -MSSO
PPT
Facebook: An Innovative Influenza Pandemic Early Warning System
PPT
PPTX
Data Applied:Decision Trees
PPTX
C,C++ In Matlab
PPT
LíRica Latina 2ºBac Lara Lozano
Retrieving Data From A Database
Introduction to Data-Applied
Data Applied: Association
Communicating simply
RapidMiner: Nested Subprocesses
Procedures And Functions in Matlab
R: Apply Functions
R Environment
PresentacióN De Quimica
XL-MINER:Prediction
Data Applied:Tree Maps
Paramount Search Partners
MySql:Basics
建築師法修正草案總說明
MED dra Coding -MSSO
Facebook: An Innovative Influenza Pandemic Early Warning System
Data Applied:Decision Trees
C,C++ In Matlab
LíRica Latina 2ºBac Lara Lozano
Ad

Similar to LISP: Macros in lisp (20)

PPTX
LISP: Program structure in lisp
PPTX
LISP:Program structure in lisp
PPTX
C language 3
PDF
User defined functions in matlab
PDF
Task Perform addition subtraction division and multiplic.pdf
PDF
Module 5.pdf
PDF
programlama fonksiyonlar c++ hjhjghjv jg
PDF
Functions-Computer programming
DOC
Functions
PPTX
Functions in Python Programming Language
PPTX
Functions in C++
PPTX
Functions in C++
PDF
USER DEFINED FUNCTIONS IN C MRS.SOWMYA JYOTHI.pdf
PPTX
User defined function in C.pptx
PDF
Cpp functions
PDF
Function in C++
PDF
functions notes.pdf python functions and opp
PPTX
Java script function
PPTX
FUNCTION CPU
PPTX
Writing command macro in stratus cobol
LISP: Program structure in lisp
LISP:Program structure in lisp
C language 3
User defined functions in matlab
Task Perform addition subtraction division and multiplic.pdf
Module 5.pdf
programlama fonksiyonlar c++ hjhjghjv jg
Functions-Computer programming
Functions
Functions in Python Programming Language
Functions in C++
Functions in C++
USER DEFINED FUNCTIONS IN C MRS.SOWMYA JYOTHI.pdf
User defined function in C.pptx
Cpp functions
Function in C++
functions notes.pdf python functions and opp
Java script function
FUNCTION CPU
Writing command macro in stratus cobol

More from DataminingTools Inc (20)

PPTX
Terminology Machine Learning
PPTX
Techniques Machine Learning
PPTX
Machine learning Introduction
PPTX
Areas of machine leanring
PPTX
AI: Planning and AI
PPTX
AI: Logic in AI 2
PPTX
AI: Logic in AI
PPTX
AI: Learning in AI 2
PPTX
AI: Learning in AI
PPTX
AI: Introduction to artificial intelligence
PPTX
AI: Belief Networks
PPTX
AI: AI & Searching
PPTX
AI: AI & Problem Solving
PPTX
Data Mining: Text and web mining
PPTX
Data Mining: Outlier analysis
PPTX
Data Mining: Mining stream time series and sequence data
PPTX
Data Mining: Mining ,associations, and correlations
PPTX
Data Mining: Graph mining and social network analysis
PPTX
Data warehouse and olap technology
PPTX
Data Mining: Data processing
Terminology Machine Learning
Techniques Machine Learning
Machine learning Introduction
Areas of machine leanring
AI: Planning and AI
AI: Logic in AI 2
AI: Logic in AI
AI: Learning in AI 2
AI: Learning in AI
AI: Introduction to artificial intelligence
AI: Belief Networks
AI: AI & Searching
AI: AI & Problem Solving
Data Mining: Text and web mining
Data Mining: Outlier analysis
Data Mining: Mining stream time series and sequence data
Data Mining: Mining ,associations, and correlations
Data Mining: Graph mining and social network analysis
Data warehouse and olap technology
Data Mining: Data processing

Recently uploaded (20)

PPTX
Understanding_Digital_Forensics_Presentation.pptx
PDF
Optimiser vos workloads AI/ML sur Amazon EC2 et AWS Graviton
PPTX
Cloud computing and distributed systems.
PDF
Mobile App Security Testing_ A Comprehensive Guide.pdf
PPTX
Programs and apps: productivity, graphics, security and other tools
DOCX
The AUB Centre for AI in Media Proposal.docx
PPTX
sap open course for s4hana steps from ECC to s4
PDF
Profit Center Accounting in SAP S/4HANA, S4F28 Col11
PDF
Chapter 3 Spatial Domain Image Processing.pdf
PPTX
Effective Security Operations Center (SOC) A Modern, Strategic, and Threat-In...
PDF
The Rise and Fall of 3GPP – Time for a Sabbatical?
PPTX
VMware vSphere Foundation How to Sell Presentation-Ver1.4-2-14-2024.pptx
PPT
“AI and Expert System Decision Support & Business Intelligence Systems”
PDF
NewMind AI Weekly Chronicles - August'25 Week I
PPTX
20250228 LYD VKU AI Blended-Learning.pptx
PPTX
Spectroscopy.pptx food analysis technology
PPTX
ACSFv1EN-58255 AWS Academy Cloud Security Foundations.pptx
PDF
Dropbox Q2 2025 Financial Results & Investor Presentation
PDF
Reach Out and Touch Someone: Haptics and Empathic Computing
PDF
Diabetes mellitus diagnosis method based random forest with bat algorithm
Understanding_Digital_Forensics_Presentation.pptx
Optimiser vos workloads AI/ML sur Amazon EC2 et AWS Graviton
Cloud computing and distributed systems.
Mobile App Security Testing_ A Comprehensive Guide.pdf
Programs and apps: productivity, graphics, security and other tools
The AUB Centre for AI in Media Proposal.docx
sap open course for s4hana steps from ECC to s4
Profit Center Accounting in SAP S/4HANA, S4F28 Col11
Chapter 3 Spatial Domain Image Processing.pdf
Effective Security Operations Center (SOC) A Modern, Strategic, and Threat-In...
The Rise and Fall of 3GPP – Time for a Sabbatical?
VMware vSphere Foundation How to Sell Presentation-Ver1.4-2-14-2024.pptx
“AI and Expert System Decision Support & Business Intelligence Systems”
NewMind AI Weekly Chronicles - August'25 Week I
20250228 LYD VKU AI Blended-Learning.pptx
Spectroscopy.pptx food analysis technology
ACSFv1EN-58255 AWS Academy Cloud Security Foundations.pptx
Dropbox Q2 2025 Financial Results & Investor Presentation
Reach Out and Touch Someone: Haptics and Empathic Computing
Diabetes mellitus diagnosis method based random forest with bat algorithm

LISP: Macros in lisp

  • 2. CL macro facility allows the user to define arbitrary functions that convert certain lisp forms into different forms before evaluating or compiling them.Macros makes it possible to write codes that are clear and elegant.In CL, macros are not functionsIn particular, macros cannot be used as the functional arguments to functions such as apply, funcall, or map.
  • 4. Macro definitiondefmacro construct is used to define a macro.Macro-function determines whether the given function is the name of the macro.macro-function symbolThe argument must be a symbol, if the function has a global function definition( macro definition) then the expansion function is returned else nil is returned.Macro-function cannot work on local variables it can only determine the global definitions.Setf is used as a macro function to install a macro as a symbols global function definitionTo add an optional environment argument to macro-function:macro-function symbol &optional env(setf (macro-function symbol) fn)
  • 5. defmacro has the same syntax as the defunHere, name is the symbol whose macro definition we are creating.Lambda-list is similar in form to a lambda-list, Forms constitute the body of the expander function.The defmacro function returns the name.defmacro name lambda-list [[ {declaration}* | doc-string ]] {form}* [Macro]
  • 6. Defmacro lambda-list keywordsSome of the lambda-list key words are: &optional, &rest, &key, &allow-other-keys, &aux&body: this is identical in function to &rest, it informs certain output-formatting and editing functions that the remainder of the form is treated as a body and should be intended accordingly.&whole: this is followed by a single variable that is bound to the entire macro-call form.&environment: this is followed by a single variable that is bound to an environment representing the lexical environment in which the macro call is to be interpreted.
  • 7. Macro-expansionIf form is a macro call, then macroexpand-1 will expand the macro call once and return two values.( the expansion and t) if form is not macro call then two values ( form and nil) are returned.A form is said to be a macro call only if it is a cons whose car is a symbol that names a macro.The macro expansion function is used for expanding a macro call.
  • 8. Macro-expansion steps followedOnce the macroexpand-1 has determined that a symbol names a macro, it obtains the expansion function for that macro.The value of the variable *macro-expand-hook* is then called as a function of three arguments: the expansion function, the form, and the environment env.The value returned from this call is taken to be the expansion of the macro call.The evaluator makes use of the macro calls as if through the use of macroexpand-1; eval also makes use of *macroexpand-hook*Macroexpand is similar to macroexpand-1, but repeatedly expands forms until it is no longer a macro call.
  • 9. DestructingTo make the destructing feature of the defmacro available use:This macro binds the variables specified in lambda-list to the corresponding values in the tree structure resulting from evaluating the expression, then executes the forms as the an implicit progn.If the result of the evaluating expression does not match the destructuring pattern, an error should be signaled.Destructuring-bind lambda-list expression {declaration}* {form}[Macro]
  • 10. Compiler macrosCompiler macros are used to transform the selective source code when the compiler is processing the code.This is similar to defmacro except the definition is not stored in the symbol function cell of name and is not seen by macroexpand-1The lambda-list may include &environment, &whole and may include destructuring.Return-form is used to exit from the function.define-compiler-macro name lambda-list {declaration | doc-string}* {form}*
  • 11. Name must be a symbol, if it has been defined as a compiler macro, then the compiler-macro-function returns the macro expansion function, else returns nil.Expander function is obtained as if by a call to compiler-macro-function on the car of the form rather than by a call to macro-function.Two values are returned, the expansion and the value if expansion is true else nil.compiler macro function name &optional envcompiler-macroexpand from &optional envcompiler-macroexpand-1 from &optional env
  • 12. Three cases when no expansion occurs:There is no compiler macro definition for the car of the form.There is such a definition but there is also a not inline declaration, either globally or in lexical environment env.A global compiler macro definition is shadowed by a local function or macro definition.
  • 13. EnvironmentsThere is a minimal set of accessors and a constructor for environments.Define-declaration is used to define all the standard declaration specifiers, with the exception of special.The function returns the interpretation of the symbol variable when it appears as a variable within the lexical environment env.Three values are returned:The first value indicates the type of definition in the env which may be :special, :lexical, :symbol-macro, :constantVariable-information variable &optional env
  • 14. 2. The second value is true if there is a local binding else it returns nil.3. The third value is an a-list containing information about the declarations that apply to the apparent binding of the variable.Returns the information about the interpretation of the function when it appears in the functional position of the lexical environment env.This function also returns three values:Type of definition or binding of the function name which is apparent in env (nil, :function, :macro, :special-form) The second value is true if the function is local else nil.The third value is an a-list containing information about declarations that apply to the apparent binding of the function.function-information function &optional env
  • 15. argument-environment env &key :variable :symbol-macro :fuction :macro :declarereturns a new environment containing the information present in enc augmented with the information provided by the key word arguments.The arguments used are::variable list of symbols that will be visible as bound variable in the new environment,.
  • 16. :symbol-macro  list of symbol macro definitions
  • 17. :function  list of function-names that will be visible as local function bindings in the new environment.
  • 18. :macro list of local macro definitions
  • 19. :declare list of declaration specifiersdefine-declaration decl-name lambda-list {form}*The macro defines a macro for the named declaration.The function defined by this macro will be called with two augments, a declaration specifier whose car is decl-name and the env argument to augment-environment.
  • 20. The function must return two values: the first value must be one of the following keywords :variable, :function, :declare
  • 21. If the first value is :variable or :function, then the second value must be a list, the elements of which are of the form (binding-name key value)
  • 22. If the first value is :declare, the second value must be a cons of the form ( key. Value)parse-macro name lambda-list body &optional envThe function is used to process a macro definition, it returns a lambda-expression that accepts two argument, a form and an environment.The function returns an object of type function that is equivalent to what would be obtained by evaluating ‘(function, lambda-expression) in a syntactic environment env.
  • 23. Visit more self help tutorialsPick a tutorial of your choice and browse through it at your own pace.The tutorials section is free, self-guiding and will not involve any additional support.Visit us at www.dataminingtools.net