Tuesday, 2 October 2012

Exercise 183: Eliminate quasiquote and unquote from the following expressions


Eliminate quasiquote and unquote from the following expressions:


  • `(1 "a" 2 #f 3 "c")
  • this table-like shape:
    `(("alan" ,(* 2 500))
      ("barb" 2000)
      (,(string-append "carl" " , the great") 1500)
      ("dawn" 2300))
  • and this second web page:
    `(html
       (head
         (title ,title))
       (body
         (h1 ,title)
         (p "A second web page")))
    where (define title "ratings").
Also write down the nested lists that the expressions produce.

Answers

There are two answers for each question - the normal list, and the nested list representation.

1)

(list 1 "a" 2 false 3 "c")


'(1 "a" 2 false 3 "c")


2)


(list (list "alan" (* 2 5000))
        (list "barb" 2000)
        (list (string-append "carl" " , the great") 1500)
        (list "dawn" 2300))
 (list "html" (list "head" (list title)) 
               (list "body" (list "h1" title) 
                            (list "p" "A second web page")))


 '(("allan" 1000)
    ("barb"  2000)
    ("carl"  1500)
    ("dawn"  3000))


3)

(cons

 "html"
 (cons (cons "head" (cons (cons "ratings" empty) empty)) (cons (cons "body" (cons (cons "h1" (cons "ratings" empty)) (cons (cons "p" (cons "A second web page" empty)) empty))) empty)))



 '("html"
    ("head" ("title" "ratings"))
    ("body" ("h1" "ratings")
            ("p"  "A second web page")))

No comments:

Post a Comment