Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Scheme Language a. Write a function level that takes a tree and a level index as arguments and returns all items from the tree that

Scheme Language

a. Write a function level that takes a tree and a level index as arguments and returns all items from the tree that exist at the given level (in a single list). A level, i, in a tree is the set of all nodes in the tree with depth = i. E.g.:

 (level 1 '(1 (2 3 4) 5 (6 (7 8) 9)))  '(1 5) (level 2 '(1 (2 3 4) 5 (6 (7 8) 9)))  '(2 3 4 6 9) (level 3 '(1 (2 3 4) 5 (6 (7 8) 9)))  '(7 8) 

b. Write a function called tree-filter for trees that is analogous to the built-in filter for flat lists (see below). This function should apply a predicate to every element of the tree, keeping only those that pass, and return the results in a tree of the same shape as the original. E.g.:

 (tree-filter even? '(1 (2 3) ((4 5) (6 7)) (((8 (9))))))   ((2) ((4) (6)) (((8 ())))) 
(define (filter predicate sequence) (cond ((null? sequence) '()) ((predicate (car sequence)) (cons (car sequence) (filter predicate (cdr sequence)))) (else (filter predicate (cdr sequence))))) 

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image_2

Step: 3

blur-text-image_3

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Climate And Environmental Database Systems

Authors: Michael Lautenschlager ,Manfred Reinke

1st Edition

1461368332, 978-1461368335

More Books

Students also viewed these Databases questions

Question

n=int (xlogb)+1 find the number of digits in 2^(350)

Answered: 1 week ago

Question

6. Conclude with the same strength as in the introduction

Answered: 1 week ago

Question

7. Prepare an effective outline

Answered: 1 week ago