Lecture 22 Macros Chris Allsman
Lecture 22 - Macros Chris Allsman
Macrosns ⊙
Mac OS Macros Macron Macarons Macaroni
Announcements ●HW6out,due Tues8/6 ●Proj4,Scheme out o Phase I/ll due Mon 8/5 o Whole project due Fri 8/9 o 1 EC point by submitting Thurs 8/8 11:59 PM PDT Midterm Regrades due 8/1
Announcements ● HW6 out, due Tues 8/6 ● Proj4, Scheme out ○ Phase I/II due Mon 8/5 ○ Whole project due Fri 8/9 ○ 1 EC point by submitting Thurs 8/8 11:59 PM PDT ● Midterm Regrades due 8/1
Review:Interpreters Representing Expressions
Review: Interpreters & Representing Expressions
Read-Eval-Print Loop (REPL) Read Lexer ..expression... value output Eval Print ,,。… input representation string string …tokens: Parser 。..年gg年。。。。。。 “(+12)” +12N 3 ["(",,“+”,1,2,“)",]Pair("+”,Pair(1,Pair(2,ni1))
input string value expression representation Read-Eval-Print Loop (REPL) Read Eval Print output string Lexer Parser tokens “(+ 1 2)” + 1 2 3 [“(“, “+”, 1, 2, “)”, ] Pair(“+”, Pair(1, Pair(2, nil)))
Demo Representing Expressions o In Scheme,we can create lists that "look like"combinations o In fact,in Scheme,expressions are lists (or primitive values) Quoting prevents evaluation of an expression Calling eval on an unevaluated expression will evaluate that value scm>‘(+12) scm>(list quotient 10 2) (+12) (quotient 10 2) scm>(eva1'(+12)) scm>(eval (list quotient 10 2)) 3 5
Representing Expressions ● In Scheme, we can create lists that “look like” combinations ○ In fact, in Scheme, expressions are lists (or primitive values) ● Quoting prevents evaluation of an expression ● Calling eval on an unevaluated expression will evaluate that value Demo scm> ‘(+ 1 2) (+ 1 2) scm> (eval ‘(+ 1 2)) 3 scm> (list 'quotient 10 2) (quotient 10 2) scm> (eval (list 'quotient 10 2)) 5
Expressions In Scheme
Expressions In Scheme
Demo Expressions As Data Recall:programs are composed of expressions,but manipulate values or data In Scheme,expressions are either primitive expressions or lists which means they're also a form of data! This means we can: Assign expressions to variables ● Pass expressions into functions Create return new expressions within functions
Expressions As Data Recall: programs are composed of expressions, but manipulate values or data In Scheme, expressions are either primitive expressions or lists - which means they’re also a form of data! This means we can: ● Assign expressions to variables ● Pass expressions into functions ● Create & return new expressions within functions Demo
Begin Demo begin is a special form takes in any number of expressions, evaluates them in order,and evaluates to the value of the final expression (begin 3 2 1) scm>(begin (define x 2)(define x (x 1))x) 3
Begin begin is a special form takes in any number of expressions, evaluates them in order, and evaluates to the value of the final expression Demo (begin 3 2 1) 1 scm> (begin (define x 2) (define x (+ x 1)) x) 3
Let Demo (let ((symbol1 expr1) Each symbol is bound to the value of the expression (symbo12 expr2) in parallel …) Evaluate to the value of the The bindings only exist when body body using the binding evaluating the body scm>(1et((x2) (y3) (+xy)) 5
Let (let ((symbol1 expr1) (symbol2 expr2) …) body) Demo scm> (let ((x 2) (y 3)) (+ x y)) 5 Each symbol is bound to the value of the expression in parallel Evaluate to the value of the body using the binding The bindings only exist when evaluating the body