5 Quote; Design in the Context of a Mini-Project
Purpose The lab introduces an extremely convenient short-hand notation for lists. It also resumes the practice session from 4 Designing Programs on Lists on list-processing programs.
Textbook references Part II: Arbitrarily Large Data Intermezzo: Quote and Unquote
Design one function per task.
Design one function per data definition.
Follow the design recipe. Know the question-and-answer games for each step.
Introduction to quote
5 minutes
Now that you have had some practice with lists we are going to discuss a useful shorthand for generating them: quote. Quote is a function that allows you to quickly write down lists of atomic data. For example, the list (list 1 2 3) can be abbreviated '(1 2 3). Quote works by applying itself to every element of the list so '(1 2 3) becomes (list '1 '2 '3). Luckily quote applied to a number just produces the number itself so we get (list 1 2 3). What happens if our list contains non-numbers? Well, quote applied to a string produces the string so '("hi" "bye") is equivalent to (list "hi" "bye").
'(hi bye)
(list 'hi 'bye)
Often we want to create not only lists but lists within lists. Quote makes this easy because every time you write an open parenthesis it interprets it as a new instance of list. Some examples are provided below:
quoted short hand |
| list long hand | ||||||||
'((a) (b) (c)) |
| |||||||||
'(() ("hi" "bye") (1 2 3)) |
|
| ||||||||
'((((1) ("hi" ("bye")) 2) 3) (a)) |
|
|
'(html (head (title "my first web page")) (body (p "Here is an unordered list:" (ol (li "one") (li "blue") (li "design guideline") (li "design recipe")))))
Sample Problem Develop at least two more examples of nested lists using quote.
Quasiquote and unquote
10 minutes
'((make-posn 1 2)(make-posn 3 4))
The solution to this problem is the same as the one that many languages now
incorporate in some form or other: a form of quoting and
unquoting. So, imagine you want to produce your first,
second, and ... sixty-seventh web page. You could do what
little kids do—
This programmer uses quasiquote instead of quote so that s/he can escape into ISL with unquote. Quasiquote works just like regular quote with atomic data but allows us the additional functionality of unquote. So `((make-posn 1 2) (make-posn 3 4)) would still produce (list (list 'make-posn 1 2) (list 'make-posn 3 4)) but we can use a comma to produce the expression we wanted: `(,(make-posn 1 2) ,(make-posn 3 4))
(define (my-ith-web-page i-th) (quasiquote (html (head (title (unquote (string-append "my " (number->string i-th) "th web page"))) (body (p "Here is an unordered list:" (ol (li "one") (li "blue") (li "design guideline") (li "design recipe")))))))) (define my-first-few-web-pages (build-list 10000 my-ith-web-page))
> (list-ref my-first-few-web-pages (random 1000))
'(html
(head
(title "my 823th web page")
(body
(p
"Here is an unordered list:"
(ol
(li "one")
(li "blue")
(li "design guideline")
(li "design recipe"))))))
The comma provides us with a mechanism of escaping the language of quote and returning to ISL to create structures or other complex data.
Sample Problem Eliminate quote, quasiquote, and unquote from the following:
`(+ 1 2)
`(,(+ 1 2))
`(list "hello" "world")
A World In Lists
50 minutes
Have you ever heard the expression "It’s like herding cats"? Well we are going to produce a cat herding simulator! In this simulation a series of cats will move across the screen in random patterns.
A cat can move in one of three ways: moving forward, turning left, or turning right. In our simulation there will be many cats moving across the screen in a series of random movements. We have provided you with some constants, data definitions, a main function, and a wishlist to get you started. Please download the file and open it in DrRacket.
Exercise 0 Pick your favorite cat picture from the web and get rid of the ugly brown circle.
Exercise 1 Use (un)quoting to develop data examples for lists of Cats.
Exercise 2 Develop a template for process elements of N and [Listof Cat].
Exercise 3 Design the function create-steps in the wishlist.
Don’t forget to reuse the template developed in exercise 2.
The provided functions include random-choice, which consumes a list of anything and picks a random item from that list.
Exercise 4 Design the function all-cats-move in the wishlist.
Don’t forget to reuse the template developed in exercise 2.
Exercise 5 Design the function out-of-steps? in the wishlist.
Please solve the exercises in order.
Code Review
30 minutes
The Teaching Assistants will pick two pairs who will explain a solution to one of the above exercises. If the presenters do not have a solution, the TAs will guide them through the production of the solution in real time.
Postscript
Many developers are beginning to understand that templates are not quite enough for these web programs. What they really want are embedded languages (with IDE support)