I am doing an exercise from EDX course based on H2DP book and am having trouble 
implementing a `find` function using the abstract function fold-elt. The 
function should find the element and return its data.

#lang htdp/isl

(define-struct elt (name data subs))
;; Element is (make-elt String Integer ListOfElement)
;; interp. An element in the file system, with name, and EITHER data or subs.
;;         If data is 0, then subs is considered to be list of sub elements.
;;         If data is not 0, then subs is ignored.

;; ListOfElement is one of:
;;  - empty
;;  - (cons Element ListOfElement)
;; interp. A list of file system Elements

(define F1 (make-elt "F1" 1 empty))
(define F2 (make-elt "F2" 2 empty))
(define F3 (make-elt "F3" 3 empty))
(define D4 (make-elt "D4" 0 (list F1 F2)))
(define D5 (make-elt "D5" 0 (list F3)))
(define D6 (make-elt "D6" 0 (list D4 D5)))

;                D6
;              /    \
;             /      \
;            D4      D5
;          /    \     |
;         /      \    |
;        F1      F2   F3

;; The abstract fold/reduce function for Element.
(define (fold-elt c1 c2 b e)
  (local [(define (fn-for-element e)  ; -> X
            (c1 (elt-name e)    ;String
                (elt-data e)    ;Integer
                (fn-for-loe (elt-subs e))))

          (define (fn-for-loe loe)    ; -> Y
            (cond [(empty? loe) b]
                   (c2 (fn-for-element (first loe))
                       (fn-for-loe (rest loe)))]))]
    (fn-for-element e)))

;; String Element -> Integer or false
;; Search the given tree for an element with the given name,
;; produce data if found; false otherwise
(check-expect (find "F3" F1) #f)
(check-expect (find "F3" F3) 3)
(check-expect (find "D4" D4) 0)
(check-expect (find "D6" D6) 0)
(check-expect (find "F3" D4) #f)
(check-expect (find "F1" D4) 1)
(check-expect (find "F2" D4) 2)
(check-expect (find "F1" D6) 1)
(check-expect (find "F3" D6) 3)

(define (find n elt)
  (local [(define (c1 name data loe)
            (if (string=? n name) data loe))]
  (fold-elt c1 ??? #f elt)))

So, what should I do in the place marked with "???"?

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.
For more options, visit https://groups.google.com/d/optout.

Reply via email to