| 10 POINTS |
Problem$^a$ 4. Develop a program that counts the number of unknowns in a list of family trees:
(define-struct unknown (optional)) (define-struct parents (mom dad)) ;; A LOFT is one of: ;; -- empty ;; -- (cons FT LOFT) ;; A FT is one of: ;; -- (make-unknown String) ;; -- (make-parents FT FT)
Solution
(define tree1 (make-unknown 'a))
(define tree2 (make-parents (make-unknown 'b) (make-unknown 'c)))
(define forest1 empty)
(define forest2 (cons tree1 (cons tree2 empty)))
;; count-tree : FT -> Number [PT 1]
;; count the unknowns in a tree [PT 1]
(define (count-tree t)
(cond ;; [PT 1 for 2 correct cond lines/quest]
[(unknown? t) 1] ;; [PT 1 for correct "recursions"]
[else (+ (count-tree (parents-mom t)) (count-tree (parents-dad t)))]))
;; count-forest : LOFT -> Number [PT 1]
;; count the unknowns in a forest [PT 1]
(define (count-forest f)
(cond ;; [PT 1 for 2 correct cond lines/quest]
[(empty? f) 0]
;; [PT 1 for correct "recursions"]
[else (+ (count-tree (first f)) (count-forest (rest f)))]))
;; Examples/Tests: [PT 2, one each]
(= (count-tree tree2) 2)
(= (count-forest forest2) 3)