Be sure to use BSL+ for this lab.
Quotation uses a new keyword, quote, to write down lists: '(1 2 3), which is translated into (list 1 2 3). That probably seems more complicated than just using list, but the key is that ’ is a shorthand for quote, so you should really write '(1 2 3). Here are some examples:
> '(1 2 3)
> '("a" "b" "c")
Quoted lists can be nested:
> '(("a" 1) ("b" 2) ("d" 4))
quote is especially useful when writing tests for functions that deal with lists.
Exercise 1 Design a function that takes a list of lists of numbers and returns a list of the sums of all the numbers in each sublist. Using ’ is encouraged.
(define x 25)
> '(23 24 x 26 27)
in the interactions area. What result do you expect?
Here is the result of the experiment:
(list 23 24 'x 25 26)
The x did not become 25. Instead, it was quoted to create 'x, which is a new type of value called a Symbol. Symbols play a role similar to that of strings; they are a great way to represent symbolic information as data.
A symbol can look like any variable, with a ’ in front. Consider another example:
> '(1 (+ 1 1) 3)
You might expect this to construct (list 1 2 3). However, following the rules for quote, you discover that '(1 (+ 1 1) 3) is short for (list '1 '(+ 1 1) '3). The quoted numbers remain numbers and the inner list is expanded, and you get (list 1 (list '+ '1 '1) 3). Again, the quoted numbers are numbers, so the final result is
(list 1 (list '+ 1 1) 3)
This means that '+ is a symbol just like 'x; it has nothing to do with the function +.
> `(1 2 3)
> `("a" "b" "c")
> `(1 ,(+ 1 1) 3)
The same example, using the , shorthand for unquote:
> `(1 ,(+ 1 1) 3)
Exercise 2 Eliminate quote, quasiquote, and unquote from the following expression so they are written with list. Check your answers in the interactions window after you have finished.
`,(+ 1 2)
'("foo" (bar ()))
Exercise 3 Do the same for the following:
`(list y 3)
`,'(+ 1 2)
`("a" ,(cons "b" `("d" "e")) "c")
move causes the turtle to move forward by a fixed number of pixels;
turn left causes the turtle to turn left by 90 degrees on the spot;
turn right causes the turtle to turn right by 90 degrees on the spot.
The goal is to design an interactive program that illustrates how a turtle moves when given a series of commands. That is, the program consumes a list of commands, sets up the turtle facing right in the center of the canvas, and then executes one of these commands per clock tick. Its return value is the last position of the turtle.
'(turn-left move turn-right move)
Our design is available on-line. Please download the program, open it in DrRacket, and read the sections on constant definitions, data representations, main function, and wish list. As you can see the wish list consists of two functions: render-turtle and execute-one-command. We would like you to design these functions. Do not hesitate to add new wishes to the wish list. We recommend that you finish execute-one-command first so that main’s tests pass. Then work on rendering.