Lab 5 Quoted Lists, Worlds
Purpose The purpose of this lab is to equip you with some conveniences in *SL programming: quote, unquote, and friends. In addition, you will get to design a simple list-based calculator.
Quote
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")
> '()
quote quotes an expression in parentheses by add list after the open parenthesis and quoting everything inside the parentheses. Quoting a number or a string results in the same number or string.
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.
Quasiquote and Unquote
(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 +.
But what if you actually wanted a list with the result of (+ 1 1) in it? For that, you would use quasiquote. quasiquote constructs lists like quote: `(1 2 3), the shorthand for which is `(1 2 3).
In most cases, quasiquote acts like quote:
> `(1 2 3)
> `("a" "b" "c")
> `()
The difference is that in quasiquote, you can also unquote to escape back to *SL.
> `(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")
Interpreting a Simple Programming Language
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.
Challenge: Random Walks
; {0,1,2} -> ACommand ; generate a command based on the given natural number n (check-expect (random-command 0) 'move) (check-expect (symbol? (random-command (random 3))) true) (define (random-command n) (cond [(= n 0) 'move] [(= n 1) 'turn-left] [(= n 2) 'turn-right]))