Question: 5. segregate Define a function that takes a procedure that executes a Boolean test on an atomic value and a list of elements as arguments.

5. segregate

Define a function that takes a procedure that executes a Boolean test on an atomic value and a list of elements as arguments. It should returns a list containing two sublists, the #irst sublist containing the elements from the original list in which each member passes the Boolean test as True, and the second sublist containing the elements from the original list in which each member passes the Boolean test as False. You may not use the built-in filter function as a helper function. You may de#ine your own helper functions. Your implementation must be recursive.

Input: A function that takes a single element and returns a Boolean, and a lists of elements.

Output: A new list with two sublists. The first sublist contains the even numbers from the original list and the second sublist contains the odd numbers.

Example:

> (segregate even? '(7 2 3 5 8)

'((2 8) (7 3 5))

> (segregate integer? '(3.0 -5.2 8 16 99.7))

'((8 16) (3.0 -5.2 99.7))

> (segregate real? '())

'(() ())

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!