正在加载图片...
rees children or 平中 入 subtrees 2 Type Definition Tree<c>= List<rree<c>>I Leaf<c> Operations leaf? sept.17,2003 16.410/16. More Scheme countleaves Strateg base case: count of an empty tree is 0 -base case: count of a leaf is 1 recursive strategy: the count of a tree is the sum of the countleaves of each child in the tree Implementation (define (countleaves (cond ((null? tree) base case ((leaf? tree)1) base case curs工 ve case (+ (countleaves (car tree)) (countleaves (cdr tree)))))) (define (leaf? x) (not (pair? x))) sept.17,2003 16.410/16. More SchemeSept. 17, 2003 7 Trees • – Tree<C> = List<Tree<C>> | Leaf<C> – Leaf<C> = C • – leaf? 2 4 6 8 2 6 8 4 root children or subtrees Sept. 17, 2003 8 countleaves – – – Implementation: ;base case ((leaf? tree) 1) ;base case (else ;recursive case (define (leaf? x) (not (pair? x))) Type Definition: Operations: • Strategy base case: count of an empty tree is 0 base case: count of a leaf is 1 recursive strategy: the count of a tree is the sum of the countleaves of each child in the tree. (define (countleaves tree) (cond ((null? tree) 0) (+ (countleaves (car tree)) (countleaves (cdr tree)))))) 16.410/16.413 More Scheme 16.410/16.413 More Scheme
<<向上翻页向下翻页>>
©2008-现在 cucdc.com 高等教育资讯网 版权所有