Common Lisp Macros Read for Input Macros Macro lifetime Macro syntax

Slides:



Advertisements
Similar presentations
09 Examples Functional Programming. Tower of Hanoi AB C.
Advertisements

Lisp. Versions of LISP Lisp is an old language with many variants Lisp is alive and well today Most modern versions are based on Common Lisp LispWorks.
C-LISP. LISP 2 Lisp was invented by John McCarthy in 1958 while he was at the Massachusetts Institute of Technology (MIT).John McCarthyMassachusetts Institute.
Lisp II. How EQUAL could be defined (defun equal (x y) ; this is how equal could be defined (cond ((numberp x) (= x y)) ((atom x) (eq x y)) ((atom y)
Functional Programming. Pure Functional Programming Computation is largely performed by applying functions to values. The value of an expression depends.
1 Programming Languages and Paradigms Lisp Programming.
Lambda Calculus and Lisp PZ03J. Lambda Calculus The lambda calculus is a model for functional programming like Turing machines are models for imperative.
Chapter 3 Functional Programming. Outline Introduction to functional programming Scheme: an untyped functional programming language.
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.
LISP Programming. LISP – simple and powerful mid-1950’s by John McCarthy at M.I.T. “ LIS t P rocessing language” Artificial Intelligence programs LISP.
Lisp Recitation (cse471/598 Fall 2007 ) Aravind Kalavagattu.
Lisp. Versions of LISP Lisp is an old language with many variants –LISP is an acronym for List Processing language Lisp is alive and well today Most modern.
CSE S. Tanimoto Macros 1 Defining Macros in Lisp Extensibility: A language is extensible if the language can be extended. New Lisp control structures.
Introductory Lisp Programming Lecture # 2 Main Topics –Basic Lisp data types –Lisp primitives –Details of list handling Cons cells (boxes) & their representation.
Artificial Intelligence and Lisp Lecture 6 LiU Course TDDC65 Autumn Semester, 2010
Symbolic Expressions (S Expressions) Syntax: Opening and Closing parenthesis having elements in between. List represented in LISP: (A B2 C3 Shahid) (A.
Functional programming: LISP Originally developed for symbolic computing First interactive, interpreted language Dynamic typing: values have types, variables.
PRACTICAL COMMON LISP Peter Seibel 1.
SchemeCOP Introduction to Scheme. SchemeCOP Scheme Meta-language for coding interpreters –“ clean ” semantics Scheme = LISP + ALGOL –simple.
Recursion. Definitions I A recursive definition is a definition in which the thing being defined occurs as part of its own definition Example: A list.
Functional Programming COMP2003 A course on functional programming using Common Lisp Dr Eleni Mangina
COMP 205 – Week 11 Dr. Chunbo Chu. Intro Lisp stands for “LISt Process” Invented by John McCarthy (1958) Simple data structure (atoms and lists) Heavy.
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.
Conditionals and Recursion "To iterate is human, to recurse divine." - L. Peter Deutsch.
Lisp: a history Developed by John McCarthy in the 1950’s. Developed by John McCarthy in the 1950’s. Only Fortran has higher “seniority” Only Fortran has.
Yu-Tzu Lin ( 林育慈 )
Digital Electronics Data Structures LISP
1 Lisp Functions –Built-in functions –Defining functions –Function Evaluation and Special Forms defun, if Control statements –Conditional if, cond –Repetition.
CSE 341, S. Tanimoto Lisp Defining Functions with DEFUN Functions are the primary abstraction mechanism available in Lisp. (Others are structures.
COP4020 Programming Languages Functional Programming Prof. Xin Yuan.
Lecture 1-2CS251: Intro to AI/Lisp II “And now for something completely different…”
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.
Lisp Functional Language or Applicative Language –Achieves its effect by applying functions, either recursively or through composition Powerful, expressive,
LISP Data Types Functional Programming Academic Year Alessandro Cimatti
Introduction to LISP. Lisp Extensible: It lets you define new operators yourself Lisp programs are expressed as lisp data structures –You can write programs.
UMBC CMSC Common Lisp II. UMBC CMSC Input and Output Print is the most primitive output function > (print (list 'foo 'bar)) (FOO BAR) The.
CSE S. Tanimoto Macros 1 Defining Macros in Scheme Extensibility: A language is extensible if the language can be extended. New Scheme control structures.
Milos Hauskrecht (PDF) Hieu D. Vu (PPT) LISP PROGARMMING LANGUAGE.
1 Variable Declarations Global and special variables – (defvar …) – (defparameter …) – (defconstant …) – (setq var2 (list 4 5)) – (setf …) Local variables.
Introduction to LISP Atoms, Lists Math. LISP n LISt Processing n Function model –Program = function definition –Give arguments –Returns values n Mathematical.
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
ISBN Chapter 15 Functional Programming Languages.
CSE 425: Functional Programming I Programs as Functions Some programs act like mathematical functions –Associate a set of input values from the function’s.
Macros CSC 358/ Outline  Homework #6  Macro Intro  Backquote  Macros  Modify macros  Read Macros.
1 Lecture 8 Shell Programming – Control Constructs COP 3353 Introduction to UNIX.
Clojure Macros. Homoiconicity All versions of Lisp, including Clojure, are homoiconic This means that there is no difference between the form of the data.
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.
Artificial Intelligence and Lisp Lecture 6 LiU Course TDDC65 Autumn Semester,
Section 15.4, 15.6 plus other materials
Defining Macros in Lisp
LISP A brief overview.
FP Foundations, Scheme In Text: Chapter 14.
Lisp Tutorial Click on Xlisp icon – you enter the interpreter
Modern Programming Languages Lecture 20 Fakhar Lodhi
Lisp: Representation of Data
Defining Macros in Lisp
Clojure Macros.
Defining Functions with DEFUN
Abstraction and Repetition
Modern Programming Languages Lecture 18 Fakhar Lodhi
Common Lisp II.
Lisp.
Defining Macros in Scheme
Lisp: Representation of Data
LISP primitives on sequences
Presentation transcript:

Common Lisp Macros Read for Input Macros Macro lifetime Macro syntax Stored in the same "slot" as a function definition “special functions” in Lisp Look just like function calls, Except – arguments are not evaluated !! Macro lifetime expansion and then evaluation Macro syntax ` - Backquote : makes them easier to write , - Comma – evaluate item inside a backquote ,@ - Splice – splices together inside a backquote &optional and &rest in argument list View expansion with macroexpand-1

Read for Input The standard function for input is read. When given no arguments, it reads from the default place, which is usually standard input. Use in a simple input function (no error checking) > (defun ask (string) (format t "~A " string) (read)) ASK > (ask "How old are you?") How old are you? 29 29

Common Lisp Macros Lisp programs that write Lisp programs! defmacro special function Expanded at compile or load time Executed at runtime

The Life of Macros Functions have one important phase they are applied, i.e. evaluated Arguments are evaluated, put on stack Function is applied to arguments Value is returned Macros have two phases: 1. expansion and 2. evaluation (of the expansion) – same as a function call

Macro basics (defmacro nil! (var) (list 'setq var 'nil)) NIL! Macro arguments are not evaluated at the macro call Macros expand into Lisp form(s) Only the final expression of the expansion is evaluated (list 'setq var 'nil); parameter inserted Becomes (setq var nil)

Macro Example > (setq a 99) ; set a’s value 99 > a ; check value ; now call our macro 1. > (nil! a) ; (list 'setq 'a 'nil) ; expansion 2. The expanded expression is evaluated ; (setq a nil) ; (what happens?) > a ; check value again NIL

Create a New Macro >(defmacro mymac (x y z) (list x (list y z))) ; (mymac x y z) expanded to (x (y z)), e.g.: > (mymac car cdr (list 'a 'b 'c)) ; expanded to (car (cdr (list 'a 'b 'c))) B > (defmacro cubeit (x) (list (quote *) x x x)) ; (cubeit n) expanded to (* n n n), e.g.: > (cubeit 3) ; expanded to (* 3 3 3) 27

Backquote Notation (defmacro nil! (var) ; without backquote (list (quote setq var (quote nil)))) (defmacro nil! (var) ; shorter with backquote `(setq ,var nil)) ` backquote specifies template prevents evaluation in expansion similar to quote in functions , inside ` evaluates the item just after the comma ,@ inside ` evaluates an item and “splices it into” the expression the item should be a list result is like append

Backquote Assume (setq a 1 b 2 c 3) In a macro body Expands into (+ 1 2 3)

More Macro Examples > (defmacro my-assign (var val) `(setq ,var ,val)) MY-ASSIGN > (defun assign-test (a b) (my-assign x a) (my-assign y b)) ASSIGN-TEST > (assign-test 3 4) 4 > x 3 > y

New IF Macro > (defmacro if2 (a b c) `(cond (,a ,b) (t ,c) ) IF2 > (if2 (atom x) (quote yes) (quote no)) YES

defun and defmacro Comparison > (defun head (x) (car x)) HEAD ; function defined > (head (quote (a red table))) A > (defmacro head (x) `(car ,x)) HEAD

Lecture Question (defmacro nif (expr pos zero neg) `(case (truncate (signum ,expr)) ( 1 ,pos) ( 0 ,zero) (-1 ,neg))) 1) What does this macro do? 2) Write this macro without using a backquote!

Backquote with “splice” ,@ (setq d (quote (some list))) `(a ,d b) -> (a (some list) b) `(a ,@d) -> (a some list) `(a ,@d b) -> (a some list b) Look at the macros in www2.hawaii.edu/janst/313/lisp/showout.lisp !

More Macro Expansion Examples > (setq a 3) 3 > '(if a is true (and a a) then) ; regular quote (IF A IS TRUE (AND A A) THEN) > `(if ,a is true ,(and a a) then) ; backquote with , (IF 3 IS TRUE 3 THEN) > (list 'if a 'is 'true (list a a) 'is 'true) ; no backquote (IF 3 IS TRUE (3 3) IS TRUE) > (list 'if a 'is 'true (and a a) 'is 'true) ; no backquote (IF 3 IS TRUE 3 IS TRUE) > (setq b '(a b c)) (A B C) > `(hello fred ,b) ; backquote (HELLO FRED (A B C)) > `(hello fred ,@b) ; backquote with ,@ (HELLO FRED A B C)

Macro Debugging macroexpand-1 expands a macro >(defmacro alpha (x y) `(beta ,x ,y)) ALPHA >(macroexpand-1 '(alpha a b)) (BETA A B) T (defmacro mexp-1 (expr) `(pprint (macroexpand-1 ',expr))) Note the quote and comma in ',expr quote ' and comma , what does that produce?

&optional in a Macro &optional works like in a function > (defmacro if2 (a b &optional c) `(cond (,a ,b) (t ,c) ) IF2 > (if2 (atom x) 'yes 'no) YES > (if2 (atom x) 'yes)

&rest in a Macro &rest works like in a function Note: Recursion > (defmacro let*2 (x &rest forms) (if (null x) `(progn ,@forms) `(let (,(car x)) (let*2 ,(cdr x) ,@forms)))) Note: Recursion