On this page:
Introduction to quote
Quasiquote and unquote
A World In Lists
Code Review
Postscript
6.11.0.4

5 Quote; Design in the Context of a Mini-Project

home work!

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

Reminders
  • 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").

Something interesting happens if we don’t provide the double quotes. The list

'(hi bye)

is equivalent to

(list 'hi 'bye)

These single quoted entities are called symbols, and they are roughly simplistic strings. Clearly, they are more convenient to write (one quote is quicker than two!) but they also come with far fewer operations than strings. For example, you cannot get part of a symbol as you can with a string (see substring), cannot find its length, and cannot combine it with other symbols (see string-append). There are two useful operations on symbol: (1) symbol?, which checks whether something is a symbol, and (2) symbol=?, which compares two symbols.

Symbols are a useful type of data when we want to enumerate something as in the data definition given below:
; 1. A Direction is one of:
;  'left
;  'right
;  'up
;  'down

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))

     

(list (list 'a) (list 'b) (list 'c))

'(() ("hi" "bye") (1 2 3))

     

(list '()
 (list "hi" "bye")
 (list 1 2 3))

'((((1) ("hi" ("bye")) 2) 3) (a))

     

(list
  (list
    (list
      (list 1)
      (list "hi" (list "bye"))
      2)
    3)
  (list 'a))

Why would anyone care about nested lists? Consider this example:
'(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")))))
This about the simplest data representation of a web page within a programming language that you can imagine. When you code for a living these days, you will often want to generate HTML code while you’re working in some programming language (such as JavaScript).

Sample Problem Develop at least two more examples of nested lists using quote.

Quasiquote and unquote

10 minutes

Quote is useful for producing lists of atomic data, but you’re in for a surprise with expressions such as

'((make-posn 1 2)(make-posn 3 4))

This produces

(list (list 'make-posn 1 2) (list 'make-posn 3 4))

and that’s not what we’d want.

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—copy the above code and modify the number—or you could become the amazing "lazy" program designer that you want to be.

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))

Once you understand the idea of quoting and unquoting, you can write a function that produces all the web pages with a single expression:
(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))

And now you can generate the code for whatever web page you want automatically:
> (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"))))))

Reload the page and pay close attention.

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)