Construct a Macro that works like an if! 1.Write out the expression as you would normally do so if it were inside define. (define-macro (if-macro condl exprl else expr2) (if cond1 expr1 expr2) 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! 1. Write out the expression as you would normally do so if it were inside define. (define-macro (if-macro cond1 expr1 else expr2) (if cond1 expr1 expr2) ) 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! 2.Put the necessary quotes .. (define-macro (if-macro cond1 expr1 else expr2) (if cond1 expr1 ,expr2) OR (list if condl exprl expr2) There are more solutions! 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! 2. Put the necessary quotes … (define-macro (if-macro cond1 expr1 else expr2) `(if ,cond1 ,expr1 ,expr2) ) 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 OR (list ‘if cond1 expr1 expr2) There are more solutions!
Construct a Macro that works like an if! 3.TEST YOUR CODE... (define-macro (if-macro cond1 exprl Evaluate operator else expr2) Apply operator to unevaluated operands perform lookups return macro body (if (print 3)(print ryan)(print cesar) Hard Evaluate the expression returned by the macro in the frame it was called in. eval (if (print 3)(print this is what gets 'ryan)(print 'cesar))) returned! ;;3 ;;ryan
3. TEST YOUR CODE … (define-macro (if-macro cond1 expr1 else expr2) `(if ,cond1 ,expr1 ,expr2) ) ;;scm>(if-macro (print 3) ;; (print 'ryan) ;; else (print 'cesar)) ;;3 ;;ryan ● 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. (eval `(if (print 3)(print ‘ryan)(print ‘cesar))) `(if (print 3)(print ‘ryan)(print ‘cesar)) this is what gets returned! Construct a Macro that works like an if!
let's try another problem!
let’s try another problem!
Code Writing:Add or Sub Define a macro which takes a conditional expression and two values. Returns the sum of the two values if the conditional is true or the difference if false. (define-macro (add-or-sub cond1 x1 x2) i your code here (add-or-sub (1 2)3 2) ;expect1 (add-or-sub (22)3 2) expect 5
Code Writing: Add or Sub Define a macro which takes a conditional expression and two values. Returns the sum of the two values if the conditional is true or the difference if false. (define-macro (add-or-sub cond1 x1 x2) ; your code here ) (add-or-sub (= 1 2) 3 2) ; expect 1 (add-or-sub (= 2 2) 3 2) ; expect 5