Hey guys,

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

For the first part
"Reformulate the data definition for S-expr
<https://www.htdp.org/2019-02-24/part_four.html#%28tech._s._expr%29> so
that the first clause is expanded into the three clauses of Atom
<https://www.htdp.org/2019-02-24/part_four.html#%28tech._atom%29> and the
second clause uses the List-of

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)]
         (... (fn-for-sl sexp))])) ;[List-of S-expr]

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

For the second part
"Now integrate the definition of SL
<https://www.htdp.org/2019-02-24/part_four.html#%28tech._sl%29> 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) (...)]
         (... (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]
         (+  (count.v2 (first sexp) sy)
             (count.v2 (rest sexp) sy))]))

You received this message because you are subscribed to the Google Groups 
"Racket Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to racket-users+unsubscr...@googlegroups.com.
To view this discussion on the web visit 

Reply via email to