正在加载图片...
PS.3.1.3: Functional Composition Write a simple Scheme program that determines the maximum depth of functional composition of a mathematical expression written in Scheme notation. For (depth (depth(expt X 2))=> 1 (depth(+(expt x 2)(expt y 2))=>2 (depth((expt x 5)(expt ((expt x 2)1)(52))=> 4 (define (depth exp) (if (pair? exp) ( 1 (apply max(map depth exp)) 0 pt.24,2003 6410/16413 Even More scheme PS.3.1. 4: Indexing Trees 入 It would be handy to have a procedure that is analogous to list-ref, but for tr would take a tree and an index, and return the part of the tree(a leaf or a subtree) at that index. For trees, indices will have to be lists of integers. Consider the tree (4 (8910) To select the element 9 out of it, we need to do something like (second(fourth tree)) nstead, we'd prefer to do (tree- ref (list 3 1)tree) (note that were using zero-based indexing as in list-ref, and that the indices come in top-down order; so an index of (3 1)means you should take the fourth branch of the main tree, and then the second branch of that subtree). As another example, the element 6 could be selected by(tree- ref (list 1 11)tree). Also, it,'s okay for the result to be a subtree, rather than a leaf. so( tree- ref (list o)tree) should return(( 2)3) pt.24,2003 16410/16413 Even more schemeSept. 24, 2003 16.410/16.413 Even More Scheme 9 PS.3.1.3: Functional Composition Write a simple Scheme program that determines the maximum depth of functional composition of a mathematical expression written in Scheme notation. For example, (depth 'x) => 0 (define (depth exp) (if )) 0 (+ 1 (apply max (map depth exp))) (pair? exp) Sept. 24, 2003 16.410/16.413 Even More Scheme 10 PS.3.1.4: Indexing Trees (((1 2) 3) (4 (5 6)) 7 (8 9 10)) (second (fourth tree)) (tree-ref (list 3 1) tree) result to be a subtree, rather than a leaf. So (tree-ref (list 0) tree) should return ((1 (depth '(expt x 2)) => 1 (depth '(+ (expt x 2) (expt y 2))) => 2 (depth '(/ (expt x 5) (expt (- (expt x 2) 1) (/ 5 2)))) => 4 It would be handy to have a procedure that is analogous to list-ref, but for trees. It would take a tree and an index, and return the part of the tree (a leaf or a subtree) at that index. For trees, indices will have to be lists of integers. Consider the tree To select the element 9 out of it, we need to do something like Instead, we'd prefer to do (note that we're using zero-based indexing, as in list-ref, and that the indices come in top-down order; so an index of (3 1) means you should take the fourth branch of the main tree, and then the second branch of that subtree). As another example, the element 6 could be selected by (tree-ref (list 1 1 1) tree). Also, it's okay for the 2) 3)
<<向上翻页向下翻页>>
©2008-现在 cucdc.com 高等教育资讯网 版权所有