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
<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
<https://www.htdp.org/2019-02-24/part_three.html#%28tech._sim-dd._list._of%29>
 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
<https://www.htdp.org/2019-02-24/part_four.html#%28tech._sl%29> into the
one for S-expr
<https://www.htdp.org/2019-02-24/part_four.html#%28tech._s._expr%29>"

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
<http://docs.racket-lang.org/htdp-langs/intermediate-lam.html#%28form._%28%28lib._lang%2Fhtdp-intermediate-lambda..rkt%29._lambda%29%29>
"

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))]))

-- 
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 
https://groups.google.com/d/msgid/racket-users/CALU5e8LvM63O0TQ8kSt8KJStHdW_ysqdYU2dUHDGuNDshO0dvg%40mail.gmail.com.

Reply via email to