Macros and Function Generators CS 480/680 – Comparative Languages.

Slides:



Advertisements
Similar presentations
Higher-Order Functions and Loops c. Kathi Fisler,
Advertisements

1 Copyright © 1998 by Addison Wesley Longman, Inc. Chapter 14 Functional Programming Languages - The design of the imperative languages is based directly.
Some non-recursive tricks. The Lambda expression. More on Let, Let*, apply and funcall.
1 Scheme and Functional Programming Aaron Bloomfield CS 415 Fall 2005.
Macro simple idea of textual substitution useful when you need a group of instructions or directives frequently.
1 Programming Languages (CS 550) Lecture Summary Functional Programming and Operational Semantics for Scheme Jeremy R. Johnson.
Functional Programming. Pure Functional Programming Computation is largely performed by applying functions to values. The value of an expression depends.
CSE 341 Lecture 16 More Scheme: lists; helpers; let/let*; higher-order functions; lambdas slides created by Marty Stepp
Functional Programming COMP2003 A course on functional programming using Common Lisp Dr Eleni Mangina
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.
1-1 An Introduction to Scheme March Introduction A mid-1970s dialect of LISP, designed to be a cleaner, more modern, and simpler version than.
Metacircular Evaluation SICP Chapter 4 Mark Boady.
CS 330 Programming Languages 10 / 14 / 2008 Instructor: Michael Eckmann.
CS 330 Programming Languages 10 / 11 / 2007 Instructor: Michael Eckmann.
CS 330 Programming Languages 11 / 18 / 2008 Instructor: Michael Eckmann.
Common Lisp Derek Debruin Mark Larue Vinh Doan. Problem Domains There was a need in the early 1960s to define a programming language whose computational.
PRACTICAL COMMON LISP Peter Seibel 1.
Chapter TwelveModern Programming Languages1 Memory Locations For Variables.
ISBN Chapter 15 Functional Programming Languages.
Functional Programming in Scheme and Lisp. Overview In a functional programming language, functions are first class objects. You can create them, put.
CS 330 Programming Languages 11 / 21 / 2006 Instructor: Michael Eckmann.
10/16/2015IT 3271 All about binding n Variables are bound (dynamically) to values n values must be stored somewhere in the memory. Memory Locations for.
Introduction to Scheme CS 480/680 – Comparative Languages “And now for something completely different…” – Monty Python “And now for something completely.
Functional Programming and Lisp. Overview In a functional programming language, functions are first class objects. In a functional programming language,
Input and Output in Scheme CS 480/680 – Comparative Languages.
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.
1 FUNCTIONS - I Chapter 5 Functions help us write more complex programs.
ISBN Chapter 15 Functional Programming Languages.
CSE S. Tanimoto Macros 1 Defining Macros in Scheme Extensibility: A language is extensible if the language can be extended. New Scheme control structures.
Copyright © Curt Hill Formatting Reals Outputs other than normal.
Basic Scheme February 8, 2007 Compound expressions Rules of evaluation Creating procedures by capturing common patterns.
Lecture on Set! And Local CS 2135 Copyright Kathi Fisler, 2002 This material requires Advanced Language Level.
Fall 2004CSI University of Ottawa Introduction to PHP Basic principles and syntax.
Scheme in Scheme 2. What’s next  Adding set!  Dynamic vs. lexical variable scope  Extending mcscheme v1 with libraries  Can mcscheme execute mcscheme?
CS 330 Programming Languages 11 / 28 / 2006 Instructor: Michael Eckmann.
1 FP Foundations, Scheme In Text: Chapter Chapter 14: FP Foundations, Scheme Mathematical Functions Def: A mathematical function is a mapping of.
Creating FunctionstMyn1 Creating Functions Function can be divided into two groups: –Internal (built in) functions –User-defined functions.
Forms Writing your own procedures CS 480/680 – Comparative 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.
PPL Syntax & Formal Semantics Lecture Notes: Chapter 2.
Today… Python Keywords. Iteration (or “Loops”!) Winter 2016CISC101 - Prof. McLeod1.
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.
CS314 – Section 5 Recitation 9
Functional Programming
CS314 – Section 5 Recitation 10
Functional Programming Languages
Basic Scheme February 8, 2007 Compound expressions Rules of evaluation
Defining Macros in Lisp
CS 326 Programming Languages, Concepts and Implementation
CSE341: Programming Languages Lecture 15 Macros
Nondeterministic Evaluation
CSE341: Programming Languages Lecture 15 Macros
FP Foundations, Scheme In Text: Chapter 14.
Functions and Macros.
CSE341: Programming Languages Lecture 15 Macros
Explicit Application of Procedures, and Explicit Evaluation
6.001 SICP Environment model
Defining Macros in Lisp
CSE341: Programming Languages Lecture 15 Macros
Clojure Macros.
CSE341: Programming Languages Lecture 15 Macros
topics interpreters meta-linguistic abstraction eval and apply
Rehearsal: Lazy Evaluation Infinite Streams in our lazy evaluator
More Scheme CS 331.
Defining Macros in Scheme
CSE341: Programming Languages Lecture 15 Macros
Parameters and Arguments
C Parameter Passing.
Presentation transcript:

Macros and Function Generators CS 480/680 – Comparative Languages

Macros2 List Structures  When we have a list like this: ’( ) we know the structure looks like this:  Likewise, when you create code like this: (+ 2 3) you end up with a structure like this:

Macros3 Creating Code  A powerful feature of Scheme is the ability to create code on the fly: (define a ‘(+ 3 2)) (list a a a) >> ((+ 2 3) (+ 2 3) (+ 2 3)) (eval (cons ‘begin (list a a a))) >> 5  begin only returns the result of the last subform evaluated. However, here (+ 2 3) was evaluated three times

Macros4 Macros  A Scheme macro is a function that: Produces a list, and then Evaluates the list  Differs from an ordinary function, in that the list that is produced is evaluated The macro returns whatever the evaluated list returns

Macros5 Macro Shortcuts  Within a macro, you can use a list template: Starts with a backquote: ` Inside the template: ,symbol – replaced by a macro parameter – replaced by a spliced parameter. That is, a parameter with the outer set of parentheses removed

Macros6 A simple example  Arguments to lambda are collected in the list: ((display “hi”) (newline))  Template creates: (begin (display “hi”) (newline) (display “hi”) (newline) (display “hi”) (newline)) (require (lib ”defmacro.ss”)) (define-macro do3 (lambda code (do3 (display "hi") (newline)) hi Notice the lack of quotes!

Macros7 A more complicated example  Suppose we wanted a function called “when”, that would take as arguments a test and multiple subforms If the test is true, all the subforms are executed Like ‘if’, but: (if (test) (begin (form1) (form2) (form3)… ) )

Macros8 A more complicated example (when (< (pressure tube) 60) (open-valve tube) (attach floor-pump tube) (depress floor-pump 5) (detach floor-pump tube) (close-valve tube)) Test Subforms

Macros9 A more complicated example (define-macro when (lambda (test. branch) (list 'if test (cons 'begin branch)))) (when (< (pressure tube) 60) (open-valve tube) (attach floor-pump tube) (depress floor-pump 5) (detach floor-pump tube) (close-valve tube))

Macros10 A more complicated example (define-macro when (lambda (test. branch) (list 'if test (cons 'begin branch)))) test = (when (< (pressure tube) 60) branch = ((open-valve tube) (attach floor-pump tube) (depress floor-pump 5) (detach floor-pump tube) (close-valve tube))

Macros11 A more complicated example (define-macro when (lambda (test. branch) (list 'if test (cons 'begin branch)))) test = (< (pressure tube) 60) branch = ((open-valve tube) (attach floor-pump tube) (depress floor-pump 5) (detach floor-pump tube) (close-valve tube)) >> (if (< (pressure tube) 60) (begin (open-valve tube) … )

Macros12 Variable Capture  Since a macro is a textual translation, it does not introduce a new local scope!  If a macro uses a local variable name (via let) that is the same as a variable of a higher scope, the higher scoped variable cannot be passed as a macro parameter Can leave quite a mess  Solution – (gensym) creates a unique symbol name that can be used with,name in a template