[racket] htdp/2e: exercise 302, difficulties

From: Daniel Bastos (dbastos at toledo.com)
Date: Wed Jan 21 12:06:39 EST 2015

;Gentlemen, I'm having difficulties with X-expressions. I decided to make
up examples for each type that appears as a way to engage myself in the
business, but I fail to be sure whether my examples are correct.

; 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.

; 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.

; 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.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.racket-lang.org/users/archive/attachments/20150121/0872a8ec/attachment.html>

Posted on the users mailing list.