[racket] htdp/2e: exercise 302, difficulties
Just a few MF-labeled comments -- Matthias
; An Xexpr.v0 (short for X-expression) is
; (cons Symbol '())
(define sv0.0 (cons 'machine empty))
(define sv0.1 (cons 'another empty))
;These v0 I'm confident.
; An Xexpr.v1 is
; (cons Symbol [List-of Xexpr.v1])
(define sv1.0 (cons 'machine empty))
; <machine>
; <initial></initial>
; <state></state>
; <state></state>
; </machine>
(define initial (cons 'initial empty))
(define state1 (cons 'state empty))
(define state2 (cons 'state empty))
(define a-machine (cons 'machine (list initial state1 state2)))
; I made up the XML example to be able to translate it piece by piece to
; make it easier. I claim now that a-machine represents the XML expression above.
; MF: yes, a-machine is an Xexpr.v2 representation of the above XML term.
; You could also use state1 twice and not define state2.
; An Xexpr.v2 is
; – (cons Symbol [List-of Xexpr.v2])
; – (cons Symbol (cons [List-of Attribute] [List-of Xexpr.v2]))
(define a-cluster (cons 'a-cluster (list a-machine a-machine)))
;Our sample data representations from above suggest this definition for Attribute:
; An Attribute is
; (cons Symbol (cons String '()))
(define a-name-attr
(cons 'name (cons "cluster x" empty)))
(define a-named-cluster
(cons 'a-named-cluster (cons (list a-name-attr) (list a-machine a-machine))))
; At first at least, it's difficult to write these expressions and be sure they conform.
; I'm using other definitions to write less long expressions.
; MF: absolute right thing to do!
; Exercise 302. Eliminate the use of List-of from the data definition Xexpr.v2. #
; I think I don't get the spirit of this exercise. If I eliminate the use
; of List-of from Xexpr.v2, how can I get lists of attributes or lists of x-expr.v2?
; But see my attempt below.
; (*) Solution
; A ListOfAttributes is
; - empty
; - (cons Attribute ListOfAttributes)
; A ListOfXexpr.v2 is
; - empty
; - (cons Xexpr.v2 ListOfXexpr.v2)
; So we can express Xexpr.v2 without the List-of operator by using these types above.
; An Xexpr.v2 is
; – (cons Symbol ListOfXexpr.v2)
; – (cons Symbol (cons ListOfAttributes ListOfXexpr.v2))
; Is this what the exercise expects? Thank you.
; MF: Perfect. The idea is to remember that [Listof X] is just an abbreviation
; for an explicit definition of List-of-X. If you are ever stuck designing a
; program that consumes [Listof X] (or containing [Listof X]), that's what you do.