On this page:
Quote (10 minutes)
Quasiquote and Unquote (15 minutes)
A World in Lists (75 minutes)
Before you go...
6.7

Lab 3 Quote

home work!

Purpose: The purpose of this lab is to equip you with some conveniences in *SL programming: quote, unquote, and quasiquote. In addition, you will get some practice creating world programs with lists.

Textbook References: Intermezzo: Quote, Unquote

Quote (10 minutes)

Sample Problem 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. Use ’ in your tests for the function.

; A List of Numbers (LoN) is one of:
; - '()
; - (cons Number LoN)
 
; A List of Lists of Numbers (LoLoN) is one of:
; - '()
; - (cons LoN LoLoN)
 
; list-sums : LoLoN -> LoN
; Produces a list of the sums of each list
(check-expect (list-sums '()) '())
(check-expect (list-sums '((15 6 10 13) () (9 11 4))) '(44 0 24))
(define (list-sums lolon)
  (cond [(empty? lolon) '()]
        [(cons? lolon)
         (cons (sum (first lolon))
               (list-sums (rest lolon)))]))
 
; sum : LoN -> Number
; Produces the sum of the numbers in the list
(check-expect (sum '()) 0)
(check-expect (sum '(2 4 12 15)) 33)
(define (sum lon)
  (cond [(empty? lon) 0]
        [(cons? lon)
         (+ (first lon) (sum (rest lon)))]))

Quasiquote and Unquote (15 minutes)

Exercise 1 Eliminate quote, quasiquote, and unquote from the following expressions so they are written with list. Check your answers in the interactions window after you have finished.

  • `,(+ 1 2)

  • '("foo" (bar ()))

  • `(list y 3)

  • `,'(+ 1 2)

  • `("a" ,(cons "b" `("d" "e"))  "c")

A World in Lists (75 minutes)

Imagine a turtle (or a pointed triangle) that marches on the order of three different commands:
  • 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.

Clearly, commands and series of commands are the two key classes of data. Intuitively, we wish to use lists such as ’(turn-left move turn-right move) to represent a series of commands. Otherwise the world-program design is relatively straightforward, following the design recipe for interactive programs.

We have provided you with some constants, some data definitions, a main function, and a wishlist of functions to get you started. Please download the file and open it in DrRacket.

Exercise 2 Develop a template for each data definition given to you in the file.

Exercise 3 Design the function execute-one-command in the wishlist. Look at the template for a World. Recall that when a template calls another template it is an indication that you need a helper function.

Exercise 4 Design the function render-turtle in the wishlist. Make sure to rotate the turtle so that the triangle points in the correct direction.

Exercise 5 Design the function no-more-commands? in the wishlist.

Exercise 6 Design a function random-command-list which, when given a natural number n, generates a list of n random commands. Here is a function to generate one command:

; random-command : {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]))

You can now call your main function on the result of random-command-list to see a turtle follow a series of random commands!

Before you go...

If you had trouble finishing any of the exercises in the lab or homework, or just feel like you’re struggling with any of the class material, please feel free to come to office hours and talk to a TA or tutor for additional assistance.