Macros Forms that the compiler expands into code ● Decide if the macro is really necessary ● Write down the syntax of the macro ● Figure out what the macro.

Slides:



Advertisements
Similar presentations
The Important Thing About By. The Important Thing About ******** The important thing about ***** is *****. It is true s/he can *****, *****, and *****.
Advertisements

Decision Structures - If / Else If / Else. Decisions Often we need to make decisions based on information that we receive. Often we need to make decisions.
A problem with functions: arguments must always have values that can be worked out Whenever we call a function we give it arguments. Lisp then works out.
10 Macros.  Lisp code is expressed as lists, which are Lisp objects  This makes it possible to write programs that would write programs  This lecture.
Extending Pattern Directed Inference Systems Algo & Data 2.
Lisp – Introduction יעל נצר מערכות נבונות סמסטר ב' תשס"ו.
Introduction to Artificial Intelligence Lisp Ruth Bergman Fall 2002.
CSE S. Tanimoto Macros 1 Defining Macros in Lisp Extensibility: A language is extensible if the language can be extended. New Lisp control structures.
CSE S. Tanimoto Explicit Function Application 1 Explicit Application of Functions, Functional Arguments and Explicit Evaluation Implicit and explicit.
PRACTICAL COMMON LISP Peter Seibel 1.
P247. Figure 9-1 p248 Figure 9-2 p251 p251 Figure 9-3 p253.
Prof. Fateman CS 164 Lecture 261 Macro Expansion/ Macro Languages Lecture 26.
Functional Programming COMP2003 A course on functional programming using Common Lisp Dr Eleni Mangina
LISP 1.5 and beyond A very quick tour. Data Atoms (symbols) including numbers – All types of numbers including Roman! (well, in the early days) – Syntactically.
TUTORIAL 2 CSCI3230 ( First Term) By Paco WONG 1.
Common Lisp Macros Read for Input Macros Macro lifetime Macro syntax
1 Lisp Functions –Built-in functions –Defining functions –Function Evaluation and Special Forms defun, if Control statements –Conditional if, cond –Repetition.
CS 355 – PROGRAMMING LANGUAGES Dr. X. Common LISP A combination of many of the features of the popular dialects of LISP around in the early 1980s A large.
ITEC 380 Organization of programming languages Lecture 5 – Functional Programming.
1. Agenda for loop Short-handed notation How to use break and continue 2.
The Loop Macro Many of the CL “functions” are actually macros (let, progn, if, etc) The most complicated macro in CL is probably the Loop macro –The Loop.
Lecture 1-2CS251: Intro to AI/Lisp II “And now for something completely different…”
PRACTICAL COMMON LISP Peter Seibel 1.
5 or more raise the score 4 or less let it rest
Macros “How can you get anything done in [other languages], I think, without macros?” - Paul Graham, 2003.
Macros You might recall from earlier that some of the statements we use are not actually functions but macros –In CL, a function evaluates all of its parameters.
CSE S. Tanimoto Lisp Defining Macros in Lisp Extensibility: A language is extensible if the language can be extended. New Lisp control structures.
Lisp Files, Arrays, and Macros CIS 479/579 Bruce R. Maxim UM-Dearborn.
Introduction to LISP. Lisp Extensible: It lets you define new operators yourself Lisp programs are expressed as lisp data structures –You can write programs.
February ,  2/16: Exam 1 Makeup Papers Available  2/20: Exam 2 Review Sheet Available in Lecture  2/27: Lab 2 due by 11:59:59pm  3/2:
CSE S. Tanimoto Macros 1 Defining Macros in Scheme Extensibility: A language is extensible if the language can be extended. New Scheme control structures.
Design of Problem Solvers (PS) using Classical Problem Solving (CPS) techniques Classical Problem Solver has 2 basic components:  Search engine (uses.
Milos Hauskrecht (PDF) Hieu D. Vu (PPT) LISP PROGARMMING LANGUAGE.
1 COSC generating functions, templates, and macros Yves Lespérance Adapted from Peter Roosen-Runge.
Basic Introduction to Lisp
Macros and general code walkers in Lisp: how useful! or, how useful? Ernst van Waning
Overview of Previous Lesson(s) Over View 3 Model of a Compiler Front End.
CSD 340 (Blum)1 For Loops See Beginning JavaScript (Paul Wilton) p. 87.
 A macro represents a commonly used group of statements in the source programming language.  The macro processor replaces each macro instruction with.
Lecture 2-1CS251: Intro to AI/Lisp II Today Quiz Announcements –Allegro for Windows checkout –Guest lecture on Thursday Today’s topics –More on macros.
PRACTICAL COMMON LISP Peter Seibel 1.
Macros CSC 358/ Outline  Homework #6  Macro Intro  Backquote  Macros  Modify macros  Read Macros.
Unit 10 Code Reuse. Key Concepts Abstraction Header files Implementation files Storage classes Exit function Conditional compilation Command-line arguments.
Clojure Macros. Homoiconicity All versions of Lisp, including Clojure, are homoiconic This means that there is no difference between the form of the data.
Feb 17, 2015 Clojure 4. Macros Code is data We have heard this before. It is what makes Lisp so amenable to the use of macros. Examples from Mastering.
Defining Macros in Lisp
Two “identical” programs
Emily Leland (Not Nick) Spring 2017
Writing Methods.
Functions and Macros.
CSE 341 Section 7 Winter 2018 Adapted from slides by Eric Mullen, Nicholas Shahan, Dan Grossman, and Tam Dang.
CSE 341 Section 2 Winter 2018 Adapted from slides by Nick Mooney, Nicholas Shahan, Patrick Larson, and Dan Grossman.
10:00.
CSE S. Tanimoto Explicit Function Application
CSE 341 Section 2 Nick Mooney Spring 2017
Adapted from slides by Nicholas Shahan and Dan Grossman
Adapted from slides by Nicholas Shahan, Dan Grossman, and Tam Dang
Lisp: Representation of Data
Defining Macros in Lisp
Clojure Macros.
A LESSON IN LOOPING What is a loop?
Nicholas Shahan Spring 2016
February , 2009 CSE 113 B.
Repetition Statements (Loops) - 2
Modern Programming Languages Lecture 18 Fakhar Lodhi
Exponents is repeated multiplication!!!
Let’s Play BINGO to Practice Expanded Form Using Exponents!
Defining Macros in Scheme
Lisp: Representation of Data
The general format of case is the following: (case <key form>
Presentation transcript:

Macros Forms that the compiler expands into code ● Decide if the macro is really necessary ● Write down the syntax of the macro ● Figure out what the macro should expand into ● Use defmacro to implement

syntax and expansion Example: (while test body) (do ((dummy nil nil)) ((not test)) body)

Defining the macro (defmacro while (test &rest body) “Repeat body while test is true.” (list* 'do '((dummy nil nil)) (list (list 'not test)) body))

Expanding the macro (macroexpand-1 '(while (< i 10) (print (* i i)) (setf i (+ 1 i)))) (DO ((DUMMY NIL NIL)) ((NOT (< I 10))) (PRINT (* I I)) (SETF I (+ 1 I)))

Backquote Notation (defmacro while (test &rest body) (let ((code '(do ((dummy nil nil)) ((not test)). body))) (subst test 'test (subst body 'body code))))

Backquote “`” is like quote “'”, but knows that what follows may contain some components that rae to be evaluated (defmacro mwhile (test &rest body) `(do ((dummy nil nil))

Some examples (setf test1 '(a test)) => (A TEST) `(this is,test1) => (THIS IS (A TEST)) `(this is.,test1) => (THIS IS A TEST) `(this => (THIS IS A TEST)