This assignment is due on April 14th.
Minor update on 04/11/02: (..) replaced by [[..]] below.
Note about the initial direction of turtles added on 04/12/02.
Please read the second chapter ``Data Abstraction'' in EOPL.
Question 1
Please consider the small language on slide 106. Assume that you want to have assignment not just as a statement but also as an operator within expressions. Example:
i := 0;
while ((i := i + 1) < 10) {
write i
}
Then we have expressions with side effects and the semantic function for expressions no longer is of type
Rewrite the semantic equation for E[[Exp1 + Exp2]] under this assumption.
Question 2
Write a Scheme procedure create-turtle that returns a function which, within its lexical closure, maintains its current state including its position on a two-dimensional plane and its orientation (just 4 directions) and supports operations for moving one unit, turning left and right, remembering a position and returning to it (nestable). In addition, it should be possible to query the current position and the minimal / maximal coordinates so far.
As there is just one function to be returned but several operations to be implemented, symbols are used to distinguish them:
(define t (create-turtle)) ; create turtle at (0,0), upward direction (t 'forward) ; let it move forward (t 'left) ; turning to the left and (t 'right) ; to the right (t 'push) ; remember current position and (t 'pop) ; restore it (t 'x) ; returns current x coordinate (t 'y) ; returns current y coordinate (t 'minx) ; return minimal x coordinate (t 'maxx) ; return maximal x coordinate (t 'miny) ; return minimal y coordinate (t 'maxy) ; return maximal y coordinate
You can either start with an upward direction, i.e. direction vector (0,1), or, if you want to get diagrams that conform to those on the slides 14 - 17, with a downward direction, i.e. direction vector (0,-1).
Note that the created turtle procedure is no longer purely functional in its programming style as it has to modify its variables using set!.
Please submit everything in one email that includes all your answers in attachments. Multiple submission emails are permitted but then only the last one is considered.