CSE 341 -- S. Tanimoto Lisp-6 - 1 Defining Macros in Lisp Extensibility: A language is extensible if the language can be extended. New Lisp control structures.

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.
1 Programming Languages and Paradigms Lisp Programming.
1 COSC3306: Programming Paradigms Lecture 11: Applicative Programming with Lisp Haibin Zhu, Ph.D. Computer Science Nipissing University (C) 2003.
Helper functions: when extra arguments are needed Consider this problem: we want a function index_items that takes a list L and gives a number to each.
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.
CSE S. Tanimoto Explicit Function Application 1 Explicit Application of Functions, Functional Arguments and Explicit Evaluation Implicit and explicit.
Symbolic Expressions (S Expressions) Syntax: Opening and Closing parenthesis having elements in between. List represented in LISP: (A B2 C3 Shahid) (A.
PRACTICAL COMMON LISP Peter Seibel 1.
Logical functions and - Logical AND function. Example: (and (> 5 4) (< 5 4)) or - Logical OR function. Example: (and (> 5 4) (< 5 4)) not - Logical negation.
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.
CSE S. Tanimoto Lisp Lisp S-Expressions: ATOMs Every Lisp object is either an ATOM or a CONS Symbols and numbers are kinds of atoms: X, APPLE,
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.
Introduction to Lisp For Scheme Users. What Makes Lisp Different? Built-in Support for Lists Automatic Storage Management Dynamic Typing First-Class Functions.
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.
CSE 341, S. Tanimoto Lisp Defining Functions with DEFUN Functions are the primary abstraction mechanism available in Lisp. (Others are structures.
Functional Programming and Lisp. Overview In a functional programming language, functions are first class objects. In a functional programming language,
Lecture 1-2CS251: Intro to AI/Lisp II “And now for something completely different…”
Lecture 6-2CS250: Intro to AI/Lisp Programming in Your Favorite Language Lecture 5-2 February 11 th, 1999 CS250.
Macros “How can you get anything done in [other languages], I think, without macros?” - Paul Graham, 2003.
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,
CS535 Programming Languages Chapter - 10 Functional Programming With Lists.
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.
You can access the members of a list with the functions car (or first) and cdr (or rest): (setf list '(a b c)) (car list) ⇒ a (first list) ⇒ a (cdr list)
Milos Hauskrecht (PDF) Hieu D. Vu (PPT) LISP PROGARMMING LANGUAGE.
CSE S. Tanimoto More-Concepts - 1 More Programming Language Concepts Currying Lazy Evaluation Polymorphism.
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
CSE S. Tanimoto Lisps's Basic Functionality 1 LISP: Basic Functionality S-expressions Conses Lists Predicates Evaluation and quoting Conditional.
CSE 341, S. Tanimoto Lisp Explicit Application of Functions and Functional Arguments In Lisp, functions can be passed as arguments to other functions.
PRACTICAL COMMON LISP Peter Seibel 1.
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.
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.
Artificial Intelligence and Lisp Lecture 6 LiU Course TDDC65 Autumn Semester,
Lisp S-Expressions: ATOMs
Defining Macros in Lisp
Modern Programming Languages Lecture 20 Fakhar Lodhi
Chapter 15 – Functional Programming Languages
Functions and Macros.
Scheme: Basic Functionality
Modern Programming Languages Lecture 20 Fakhar Lodhi
CSE S. Tanimoto Explicit Function Application
Explicit Application of Procedures, and Explicit Evaluation
Functional Programming Concepts
Lisp: Using Functions as Data
Functional Programming Concepts
Lisp: Representation of Data
Defining Macros in Lisp
Clojure Macros.
Defining Functions with DEFUN
Abstraction and Repetition
Functional Programming: Lisp
Lisp: Using Functions as Data
LISP: Basic Functionality
Functional Programming Concepts
LISP: Basic Functionality
Common Lisp II.
LISP: Basic Functionality
Lisp.
Defining Macros in Scheme
Lisp: Representation of Data
Presentation transcript:

CSE S. Tanimoto Lisp Defining Macros in Lisp Extensibility: A language is extensible if the language can be extended. New Lisp control structures can be created using macros. A macro form is evaluated in a special way: First the macro form is expanded by applying the macro-expansion function (given in the definition) to the arguments. Then the resulting expression is evaluated again.

CSE S. Tanimoto Lisp Example: PUSH (actually built-in) > (defmacro push (element stack) (list 'if (list 'null stack) (list 'setq stack (list 'quote (list element))) (list 'setq stack (list 'cons element stack)) ) )

CSE S. Tanimoto Lisp Macro Expansion The macro expansion function is applied to the macro arguments. We can see these intermediate results if we use the built-in function MACROEXPAND. > (macroexpand '(push 5 s)) (IF (NULL S) (SETQ S '(5)) (SETQ S (CONS 5 S))) T ;2nd value is T since a macro form was expanded

CSE S. Tanimoto Lisp Full Macro-form Evaluation First the form is fully expanded, and then the resulting form is evaluated. > (setq s nil) NIL > (push 5 s) (5) > (push '(next element) s) ((NEXT ELEMENT) 5)

CSE S. Tanimoto Lisp Example: SET-TO-ONE Takes any number of arguments, which must be symbols, and gives each the value 1. > (defmacro set-to-one (&rest symbols) (append '(progn) (mapcar #'(lambda (s) (list 'setq s 1)) symbols))) > (macroexpand '(set-to-one x y z)) (PROGN (SETQ X 1) (SETQ Y 1) (SETQ Z 1)) T > (set-to-one x y z) 1 > y 1

CSE S. Tanimoto Lisp Example: TWICE Takes any number of forms and evaluates them all once and then all again. > (defmacro twice (&rest forms) (append '(progn) forms forms) ) TWICE > (twice (format t "Macros are powerful~%") (format t "Aren’t they?~%") ) Macros are powerful Aren’t they? Macros are powerful Aren’t they? NIL >

CSE S. Tanimoto Lisp Backquote and Comma Syntax Allows the body of a macro to look like the expanded form. > (defmacro push (element stack) ‘(if (null,stack) (setq ',stack '(,element)) (setq,stack (cons,element,stack)) ) ) >(macroexpand '(push 5 s)) (IF (NULL S)(SETQ 'S '(5))(SETQ S (CONS 5 S))) T Backquote is like QUOTE but it allows subexpressions preceded by a comma to be evaluated.

CSE S. Tanimoto Lisp Example: ENQUEUE Like PUSH, but puts the new element at the end of the list. > (defmacro enqueue (item lst) ‘(if (null,lst) (setq ',lst '(,item)) (nconc,lst (list,item)) ) ) > (setq q ‘(a b c)) (A B C) > (enqueue ‘d q) (A B C D)

CSE S. Tanimoto Lisp Example: SELECT Each clause is a list that begins with a value that might equal that of OBJECT. These value are tested in turn, and the first one that is equal to OBJECT has its remaining clause elements evaluated, and the value of the last of these is returned. > (select 5 (4 "too small") (5 "just right" "five") (6 "six") ) "five"

CSE S. Tanimoto Lisp Example: SELECT Note that the backquote doesn’t have to be at top-level. Also note the use of the dot (.) which means that the list that follows should be spliced into the current list. (defmacro select (object &rest clauses) (append '(cond) (mapcar #'(lambda (clause) `((equal (first ',clause),object).,(rest clause)) ) clauses)))