Practicum 3
Due: Thursday, 14 February, beginning of class.
Objectives:
- Deep Recursion.
- functions as arguments.
Reference: The Scheme Programming Language,
R. Kent Dybvig, Addison-Wesley, 2003.
Requirements:
- Write solutions to the following problems in the definitions window
of DrScheme. Save them in a
file and name the file with your last name and a .scm extension.
Drag the file into
the user/faculty/barrj/CS 321/TurnIn/Practicum 3 folder.
- In the file that you turn in, place all of the test calls given
in the problem. This way, when your file is executed the solutions should
appear automatically in the interactions window.
- You must turn in a hard copy of your functions.
- You may not use any imperative features like set!
or loops in any of your solutions.
- You must work individually!
Practicum:
- Define a function called highest that takes as its arguments a
number x and a list of numbers, ls. The list may contain numbers
and lists of numbers. You must return a list that contains all numbers in ls
that are greater than x. If the list contains a list, your answer must
contain that same list (but, of course, that list must contain only numbers
greater than x. Test data:
- (highest(3 '(1 2 3 4 5)) returns (4 5)
- (highest(10 '(20 30 10 (5 40) 7 25 (50 9 3 55) 4) returns
(20 30 (40) 25 (50 55))
- (highest(23 '()) returns '()
- (highest(33 '(10 20 30)) returns '()
- Define a function sandwich-1st that takes three paramters, two items,
x and y, and a list, ls. Replace every occurrence of two
successive y's with y x y. Note that your halting condition will
be more complicated. You'll have to detect both an empty list and a list with
only one item. You may assume that there are no lists nested inside the
parameter ls.
Test your function on:
- (sandwich-1st 'a 'b '(b c d b b e f b b g) will return '(b c d b a b e f b a b g)
- (sandwich-1st 'a 'b '(a b c d e) will return (a b c d e)
- (sandwich-1st 'a 'b '() will return ()
- (sandwich-1st 'a 'b '(a) will return (a)
- (sandwich-1st 'a 'b '(bbb)) will return (b a b a b)
- Now expand your function sandwich-lst to a new function sandwich2-lst
that works when the parameter ls may contain lists. Test your function on the
following examples.
- (sandwich-1st 'a 'b '(b c d b b e f b b g) will return '(b c d b a b e f b a b g)
as in the previous problem.
- (sandwich2-1st 'a 'b '(a b c d e) will return (a b c d e)
- (sandwich2-1st 'a 'b '() will return ()
- (sandwich2-1st 'a 'b '(a) will return (a)
- (sandwich2-1st 'a 'b '(bbb)) will return (b a b a b)
- (sandwich2-lst 'a 'b '(bbb (bbb) bbb)) will return
(b a b a b (b a b a b) b a b a b)
- (sandwich2-lst 'a 'b '(bbb () a)) will return (b a b a b () a)