scheme - Mutual recursion in racket, making cond do two things -
scheme - Mutual recursion in racket, making cond do two things -
i have 2 definitions, family tree , person.
; family-tree is: ; (make-person list-of-family-tree symbol number symbol) ; person is: ; (define-struct person [children name date eyes])
i need create "mutually recursive" function counts number of descendants in tree (including person). can't figure out how create cond more 1 thing if status met.
i.e.:
(define (count-descendants person1) (cond [(empty? (person-children person1)) +0] [else (count-descendants (first (person-children person1)))/*also +1 here*/ (count-descendants (rest (person-children person1)))/*also +1 here*/]))
any thought how recursively phone call function on other parts of list, , add together one?
what inquire accomplished begin
expression. don't need here. need combine result of 2 recursive calls. in case, need add together 1 (the current person) result of calling count-descendants
on every child. error in function utilize first
, rest
person-children
function not designed handle list of persons. when phone call on empty, error because can't person-children
of empty. finally, in case person doesn't have children, believe should still counted, homecoming 1 in case. adding up, must end this:
(define (count-descendants person1) (cond [(empty? (person-children person1)) 1] [else (+ 1 (foldl + 0 (map count-descendants (person-children person1))))]))
in here, utilize map
count descendants of children of person1, , foldl
add together result.
recursion scheme conditional racket mutual-recursion
Comments
Post a Comment