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.

Slides:



Advertisements
Similar presentations
11-Jun-14 The assert statement. 2 About the assert statement The purpose of the assert statement is to give you a way to catch program errors early The.
Advertisements

Some non-recursive tricks. The Lambda expression. More on Let, Let*, apply and funcall.
Scheme in Scheme. Why implement Scheme in Scheme  Implementing a language is a good way to learn more about programming languages  Interpreters are.
CSE 3341/655; Part 4 55 A functional program: Collection of functions A function just computes and returns a value No side-effects In fact: No program.
Recap 1.Programmer enters expression 2.ML checks if expression is “well-typed” Using a precise set of rules, ML tries to find a unique type for the expression.
1 Programming Languages (CS 550) Lecture Summary Functional Programming and Operational Semantics for Scheme Jeremy R. Johnson.
1 Programming Languages and Paradigms Lisp Programming.
Events Part III The event object. Learning Objectives By the end of this lecture, you should be able to: – Learn to use the hover() function – Work with.
GoogleTest Primer. Outline Basic Concepts Assertions Basic Assertions Binary Comparison String Comparison Floating-Point Comparison Simple Tests Test.
Functional Programming COMP2003 A course on functional programming using Common Lisp Dr Eleni Mangina
Lisp – Introduction יעל נצר מערכות נבונות סמסטר ב' תשס"ו.
Six compound procedures and higher-order procedures.
ITERATIVE CONSTRUCTS: DOLIST Dolist is an iterative construct (a loop statement) consisting of a variable declaration and a body The body states what happens.
Functional Programming COMP2003 A course on functional programming using Common Lisp Dr Eleni Mangina
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.
LISP primitives on sequences FIRST (or CAR) and REST (or CDR) take lists apart. Consider the list (First day of the semester). * (first '(First day of.
Symbolic Expressions (S Expressions) Syntax: Opening and Closing parenthesis having elements in between. List represented in LISP: (A B2 C3 Shahid) (A.
Javascript II Expressions and Data Types. 2 JavaScript Review programs executed by the web browser programs embedded in a web page using the script element.
CSC 160 Computer Programming for Non-Majors Lecture #7: Variables Revisited Prof. Adam M. Wittenstein
PRACTICAL COMMON LISP Peter Seibel 1.
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.
CMSC 202 Exceptions. Aug 7, Error Handling In the ideal world, all errors would occur when your code is compiled. That won’t happen. Errors which.
Common Lisp Macros Read for Input Macros Macro lifetime Macro syntax
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.
Functional Programming in Scheme and Lisp. Overview In a functional programming language, functions are first class objects. You can create them, put.
Functional Programming and Lisp. Overview In a functional programming language, functions are first class objects. In a functional programming language,
SWE 619 © Paul Ammann Procedural Abstraction and Design by Contract Paul Ammann Information & Software Engineering SWE 619 Software Construction cs.gmu.edu/~pammann/
Chapter Twenty-ThreeModern Programming Languages1 Formal Semantics.
Functional Programming Universitatea Politehnica Bucuresti Adina Magda Florea
Formal Semantics Chapter Twenty-ThreeModern Programming Languages, 2nd ed.1.
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…”
Macros “How can you get anything done in [other languages], I think, without macros?” - Paul Graham, 2003.
Lisp Functional Language or Applicative Language –Achieves its effect by applying functions, either recursively or through composition Powerful, expressive,
Introduction to ACL2 CS 680 Formal Methods for Computer Verification Jeremy Johnson Drexel University.
LISP Data Types Functional Programming Academic Year Alessandro Cimatti
Building user-defined functions: the progressive envelopment technique The idea: define combinations of LISP primitives through a sequence of experiments.
The Functions and Purposes of Translators Syntax (& Semantic) Analysis.
PRACTICAL COMMON LISP Peter Seibel 1.
UMBC CMSC Common Lisp II. UMBC CMSC Input and Output Print is the most primitive output function > (print (list 'foo 'bar)) (FOO BAR) The.
PRACTICAL COMMON LISP Peter Seibel 1.
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.
Introduction to LISP Atoms, Lists Math. LISP n LISt Processing n Function model –Program = function definition –Give arguments –Returns values n Mathematical.
Basic Introduction to Lisp
Macros and general code walkers in Lisp: how useful! or, how useful? Ernst van Waning
PRACTICAL COMMON LISP Peter Seibel 1.
Google C++ Testing Framework Part 2: Assertion. Concepts A test case contains one or many tests. ◦ You should group your tests into test cases that reflect.
Announcements Assignment 2 Out Today Quiz today - so I need to shut up at 4:25 1.
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.
1 Outline Review Introduction to LISP Symbols and Numbers Lists Writing LISP Functions LISPWorks.
Operational Semantics of Scheme
Original material by Eric Grimson
Conditions and Ifs BIS1523 – Lecture 8.
The Metacircular Evaluator
Lecture 23 Pages : Separating Syntactic Analysis from Execution. We omit many details so you have to read the section in the book. The halting.
The Metacircular Evaluator
Lecture 25: Metalinguistics > (meval '((lambda (x) (* x x)) 4)
The Metacircular Evaluator (Continued)
Adapted from slides by Nicholas Shahan and Dan Grossman
6.001 SICP Variations on a Scheme
Nicholas Shahan Spring 2016
Peter Seibel Practical Common Lisp Peter Seibel
Modern Programming Languages Lecture 18 Fakhar Lodhi
topics interpreters meta-linguistic abstraction eval and apply
Common Lisp II.
Rehearsal: Lazy Evaluation Infinite Streams in our lazy evaluator
LISP primitives on sequences
Presentation transcript:

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 the values of those arguments, and places those values into the body of the function. (defun min(x y) (if (< x y) x y)) >(min (+ 3 3) (- 5 1)) (Eval is the name of the lisp function that works out the value of an expression.) But what if we don’t want lisp to work out argument values? (eval (+ 3 3)) (eval (if (< 6 4) 6 4))) (eval (– 5 1)) 64 4

Suppose we want a function SET-DEF which takes a variable name and a value as arguments, and does the following: “if this variable has a value, do not change it, but return the value; if it does not yet has a value, assign this value to it, and return that value” > (set-def x 6) 6 > x 6 > (set-def x 12) 6 > x 6 However, trying to define SET-DEF as a simple DEFUN function will fail, since such a function will try to evaluate all its arguments and hence would evaluate the variable name we are trying to alter. If the variable had no value, the function would end with an error message. To do something like this, we need a new construct: a MACRO. Example

Macros and evaluation A macro is a user-definable Lisp procedure which operates by rearranging its arguments into the form of piece of Lisp text, and then getting the system to evaluate that text. Macros are defined using a very similar notation to that used for functions, except that the Lisp keyword DEFMACRO is used instead of DEFUN A Macro is not a function: it does not call eval to work out the values of its arguments. Instead, a macro writes a piece of lisp code (quoting keywords and function names in that code), places its arguments in the proper position in that code, and then calls eval to work out the value of that code. The idea of a macro as something that writes a piece of text that has the form of code, and then runs (or evaluates) that code, is a general one in many languages.

arguments EVAL Body of function result Ordinary function calling arguments Body of macro result Lisp expression Calling a macro EVAL

Trying set-def without a macro (defun set-def (var val) (setf var val) ) Various things go wrong with this: > (set-def x 6) ERROR: Unbound variable – X We could try changing it slightly by using the function “boundp” which checks if its argument is currently bound to some value; (defun set-def (var val) (cond ( (boundp var) var) ( t (set var val)))) This line is intended to mean “if var is bound to some value, then return the value of var ” This is better, but it still won’t work, and for the same reason: if we try to call it with an unbound variable, we’ll get an error: the arguments to a function always must be bound to a value.

(defmacro set-def (var value) (list ‘cond (list(list ‘boundp (list ‘quote var)) var ) (list ‘t (list ‘setf var value) ) ) ) This macro uses quotes (‘) and the incoming variables to make up a lisp expression. After that expression is constructed, it is evaluated.

Set-def using a macro (defmacro set-def (var value) (list ‘cond (list (list ‘boundp (list ‘quote var)) var ) (list ‘t (list ‘setf var value) ) )) Suppose var=x and value=6. Then the line (list (list ‘boundp (list ‘quote var)) var ) will turn into ( (boundp ‘x) x) The line (list ‘t (list ‘setf var value) ) will turn into ( t (setf x 6) ) And the whole body of the macro will turn into (cond ( (boundp ‘x) x) (t setf x 6)) If we then evaluate (cond ( (boundp ‘x) x) (t setf x 6)), we will get the value of x (if x is bound) or we will setf x to 6 (otherwise)

Macros for checking input (defmacro set-def (var value) (cond((not (symbolp var)) (error “Non-symbol first argument to SET-DEF: ~s” var)) (t(list‘cond (list (list ‘boundp (list ‘quote var)) var) (list ‘t(list ‘setf var value)) ))) symbolp takes something and checks whether it has the syntactic form of a symbol (that is, whether it is suitable to be a variable name) Notice that we can use cond and all other components of our functions as normal inside a macro. In addition to using those expressions, we quote them in making up the expression that the macro evaluates to.

WRITING MACROS Write out a sample call of the macro you would like to have, using some illustrative arguments; Then, write out the slab of Lisp code you would like this sample call to be transformed into Decide what part of this sample text should be arguments, as they will vary for each use of the macro and give them symbolic names Lastly, try to write down that transformed form using quotes for the lisp expressions that are part of that code, and including the macro arguments.