HTDP2e Exercise 320 help

40 views
Skip to first unread message

Aron-Zvi

unread,
Mar 11, 2020, 1:55:01 PM3/11/20
to Racket Users
Hey guys,

I'm working on Exercise 320 and I am not sure if I am in the right direction.

For the first part 
"Reformulate the data definition for S-expr so that the first clause is expanded into the three clauses of Atom and the second clause uses the List-of abstraction"
 
I get the following Data definition and templates. Is this correct?

; An S-expr is one of:
; – Number
; – String
; – Symbol
; – [List-of S-expr]

(define SEXP0 3)
(define SEXP1 "eff")
(define SEXP2 's)
(define SEXP3 '())
(define SEXP4 (cons 'hello (cons 20.12 (cons "world" '()))))
(define SEXP5 (cons (cons 'hello (cons 20.12 (cons "world" '())))
                    '()))

#;
(define (fn-for-sexp sexp)
  (cond [(number? sexp) (... sexp)]
        [(string? sexp) (... sexp)]
        [(symbol? sexp) (... sexp)]
        [else
         (... (fn-for-sl sexp))])) ;[List-of S-expr]

#;
(define (fn-for-sl sl)
  (cond [(empty? sl) (...)]
        [else
         (... (fn-for-sexp (first sl))   ;S-expr
              (fn-for-sl (rest sl)))]))  ;SL

For the second part
"Now integrate the definition of SL into the one for S-expr"

I get the following data definition and template. My count function based of the template works (see third part below) but this looks a bit odd so am not sure of this

; An S-expr.v2 is one of:
; – Number
; – String
; – Symbol
; – '()
; – (cons S-expr.v2 S-expr.v2)

(define SEXP-V2-0 4)
(define SEXP-V2-1 's)
(define SEXP-V2-2 "ddd")
(define SEXP-V2-3 empty)
(define SEXP-V2-4 '(d kk (3 "4" (1 "gg" f))))
(define SL0 empty)
(define SL1 '(1 s "d"))
(define SL2 '(1 s (ff 4 ("dd" f)) "d"))

#;
(define (fn-for-sexp.v2 sexp)
  (cond [(number? sexp) (... sexp)]
        [(string? sexp) (... sexp)]
        [(symbol? sexp) (... sexp)]
        [(empty? sexp) (...)]
        [else
         (... (first sexp)    ;S-expr.v2
              (rest sexp))])) ;S-expr.v2

and finally the third part
"Simplify count again. Consider using lambda"

My final count is as below and I do not see how I can simplify more or use lambda. Can I get a hint?

; S-expr.v2 Symbol -> N
; counts all occurrences of sy in sexp
(check-expect (count.v2 'world 'hello) 0)
(check-expect (count.v2 '(world hello) 'hello) 1)
(check-expect (count.v2 '(((world) hello) hello) 'hello) 2)

;(define (count.v2 sexp sy) 0) ;stub

(define (count.v2 sexp sy)
  (cond [(number? sexp) 0]
        [(string? sexp) 0]
        [(symbol? sexp) (if (symbol=? sexp sy) 1 0)]
        [(empty? sexp) 0]
        [else
         (+  (count.v2 (first sexp) sy)    
             (count.v2 (rest sexp) sy))]))










Ben Greenman

unread,
Mar 12, 2020, 11:40:29 AM3/12/20
to Racket Users
Those data definitions seem to match what the exercise is asking for.
But I think the second one is not what the exercise intends because
there are some data examples that match the 2nd definition but not the
1st.

My guess is that "integrate the definition of SL" means something less
drastic than inlining, and that the goal is to use a list function +
lambda to simplify.
Reply all
Reply to author
Forward
0 new messages