Macros
Macros
Quoting Quasi-quoting 'Quoting:prevents executing a following expression scm>'(1 2)same as (quote (1 2)) (+12) scm>(+12) 3 Quasiquoting:allows for user to specify if any sub- expression within the list should be unquoted,and hence, looked up. scm>(define x 2) 0 an example... X scm> ×3) (123) ???? OR scm> ,×3) (1×3) ????
Quoting & Quasi-quoting ● 'Quoting: prevents executing a following expression ● `Quasiquoting: allows for user to specify if any subexpression within the list should be unquoted, and hence, looked up. ○ an example ... scm> (define x 2) x scm> `(1 x 3) (1 x 3) scm> `(1 ,x 3) (1 2 3) (1 2 3) OR (1 x 3) ???? ???? scm> '(+ 1 2) ;same as (quote (+ 1 2)) (+ 1 2) scm> (+ 1 2) 3
How to Evaluate operator evaluate Apply operator to unevaluated operands macros? ● perform lookups return macro body ● Hard Evaluate the expression returned by the macro in the frame it was called (can use to in. test your code) scm>(define-macro (twice f) this is what gets (begin (print 2)(print 2)) returned! twice scm> (eval (begin (print 2)(print 2))) any questions so far?
How to evaluate macros? (can use to test your code) ● Evaluate operator ● Apply operator to unevaluated operands ● perform lookups & return macro body ● Hard Evaluate the expression returned by the macro in the frame it was called in. scm> (define-macro (twice f) `(begin ,f ,f)) twice scm> (twice (print 2)) 2 2 (eval `(begin (print 2)(print 2))) this is what gets `(begin (print 2)(print 2)) returned! any questions so far?
1)write out the expression as you would normally do so if it were inside define. but how do 2)Put the necessary quotes/lists... we write a a)do not quote things that you macro? want to look up in that frame b)Do a hard eval(...)on expressions you might want to evaluate right there! c)quote things that you don't want to look up/evaluate initially
but how do we write a macro? 1) write out the expression as you would normally do so if it were inside define. 2) Put the necessary quotes/lists … a) do not quote things that you want to look up in that frame b) Do a hard eval(...) on expressions you might want to evaluate right there! c) quote things that you don’t want to look up/evaluate initially
Construct a Macro that works like an if! (define-macro (if-macro cond1 exprl else expr2) ;;scm>(if-macro (print 3)(print 'ryan)else (print 'cesar)) ;;3 None is a truthy value in scheme! ;iryan Tips! 1)Write out the expression as you would normally do so if it were inside define. 2)Put the necessary quotes/lists... a)Do not quote things that you want to look up in that frame b)Do a hard eval(...)on expressions you might want to evaluate right there! c)Quote things that you want to evaluate only when the macro is actually called
Construct a Macro that works like an if! (define-macro (if-macro cond1 expr1 else expr2) ) ;;scm>(if-macro (print 3) (print 'ryan) else (print 'cesar)) ;;3 ;;ryan None is a truthy value in scheme! Tips! 1) Write out the expression as you would normally do so if it were inside define. 2) Put the necessary quotes/lists … a) Do not quote things that you want to look up in that frame b) Do a hard eval(...) on expressions you might want to evaluate right there! c) Quote things that you want to evaluate only when the macro is actually called