Language: Scheme Using define-syntax, create a switch statement. The statement m
ID: 3755591 • Letter: L
Question
Language: Scheme
Using define-syntax, create a switch statement. The statement must work like the example below.
Try your best to use syntax-rule ()
Sample usage:
(define x 99)
(switch x
[3 (displayln "x is 3")]
[4 (displayln "x is 4")]
[5 (displayln "x is 5")]
[default (displayln "none of the above")])
This example should end up displaying "none of the above" since x and 99 were never switched.
(
define x 99)
(switch x
[3 (displayln "x is 3")]
[4 (displayln "x is 4")]
[99 (displayln "x is 99")]
[default (displayln "none of the above")])
This example should display "x is 99".
Explanation / Answer
import math def myfun2(x): return 5.68 * x - 5.0 dispatchTable = { "sin": math.sin, "cos": math.cos, "exp": math.exp, "tan": math.tan, "myfun": lambda x: 10.2 * x - 5.0, "myfun2": myfun2 } >>> dispatchTable["sin"](math.pi) 1.2246467991473532e-16 >>> dispatchTable["cos"](math.pi) -1.0 >>> dispatchTable["tan"](math.pi) -1.2246467991473532e-16 >>> dispatchTable["exp"](math.pi) 23.140692632779267 >>> >>> >>> dispatchTable["exp"](1.0) 2.718281828459045 >>> >>> >>> dispatchTable["myfun"](1.0) 5.199999999999999 >>> dispatchTable["myfun2"](1.0) 0.6799999999999997
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.