Due by class time Thursday, November 21
Download the file assign17.scm and use it as your starting point for this assignment. To use the autotester program for this assignment, download hw17-tester.scm (and auto-tester-base.scm if you need it) and put them in the same folder as assign17.scm. Then uncomment the line (require "hw17-tester.scm") at the top of assign17.scm.
Add a new "shorthand" version of def expressions to your language. Shorthand definitions are only used for defining functions, and have the following syntactic form:
(def (name param1 param2 ...) : body1 body2 ...)which is equivalent to:
(def name (fun (param1 param2 ...) : body1 body2 ...))For example, the following function definitions are equivalent:
Standard syntax Shorthand syntax (def even? (def (even? n) : (fun (n) : (if (n == 0) (if (n == 0) then True then True else (odd? (n - 1)))) else (odd? (n - 1))))) (def hello (def (hello x y z) : (fun (x y z) : (print "Hello" x) (print "Hello" x) (print "Hello" y) (print "Hello" y) (print "Hello" z)) (print "Hello" z)))The shorthand version saves a line of code (and an extra set of parentheses), and may be easier to read. Implement the shorthand version of def as a macro in your interpreter, according to the above expansion rule. To do this, define two new functions: (def-shorthand-expression? x), which tests if x is a valid shorthand def-expression, and (def-shorthand-expander exp) that takes a valid shorthand expression and expands it into the standard form. Then add a line to m to handle shorthand definitions. For example:
==> (def (fact n) : (if (n == 0) then 1 else (n * (fact (n - 1))))) ==> (fact 5) 120
Test cases:
(test: def-shorthand-expression?)
(test: def-shorthand-expander)
Add for-loops to your language, with the following syntax:
(for var in exp : body ...)
One or more body expressions may appear after the : symbol. To evaluate a for loop, exp is evaluated once. The result should be a list (if it isn't, your interpreter should generate an appropriate error message). Then, on each cycle of the loop, var is bound to the next element of the list in turn, and the body expressions are evaluated in order. The final value returned is the symbol done. Examples:
==> (for n in (list 1 2 3 4) : (print (n * n))) 1 4 9 16 done ==> (with [total = 0] : (for x in (list 1 2 3 4 5) : (print "x is" x) (total := (total + x)) (print "total is" total))) x is 1 total is 1 x is 2 total is 3 x is 3 total is 6 x is 4 total is 10 x is 5 total is 15 done
Test cases: (test: run)
Submit your completed assign17.scm file using the Homework Upload Site. Make sure to include your name and the assignment number in a comment at the top of your file.
If you have questions about anything, don't hesitate to ask!