There is a few important functions being used in this tree diagram section.
1. structure of genetic tree
(define-struct gtree (label left right))2. the branches - defining the leaves
(define leaf1 (make-gtree "ACTG" empty empty))3. defining the small trees
(define tree1 (make-gtree "AAA" leaf1 leaf2))4. to "harvest" the tree (knowing the branches). This is an example of recursion.
(define (harvest G)It as well took me a while to understand the function "depth".
(cond
[(not (gtree? G)) empty]
[else
(append
(list (gtree-label G))
(harvest (gtree-left G))
(harvest (gtree-right G)))]))
However, it is easier to interpret if you think it as counting for different layers.
(define (depth L)For example,
(cond
[(not (list? L)) 0]
[else (add1 (apply max (map depth L)))]))
(check-expect (depth (list 1 (list 2) 3)) 2)list 1 and 3 --> 1
list 2 --> 1
total = 2
Recursion is not a really hard chapter, yet, it takes one lots of time to understand or figure out what's happening, it is just complicated sometimes and one can get confused or lost very easily.
No comments:
Post a Comment