Practicum 4
Due: Thursday, 21 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 may not use the built-in function map
in any of your solutions.
- You must work individually!
Practicum:
- Define a function doThree that takes three paramters f, g and
h, all functions,
and returns a function that takes two arguments and constructs a list that contains
the results of applying f, g and
h to those
arguments. Test your function on:
- ((doThree + - *) 3 5) will return (8 -2 15)
- ((doThree cons append equal?) '(a) '(c d)) will return (((a) c d) (a c d) #f)
- ((doThree > < =) 23 8) will return (#t #f #f)
- Define a function mapToThree that takes four parameters, f, g,
lst1, and lst2 and maps the two lists to the two parameters. The result
returned must be a single list. In the list should be the result of applying f to
the first item of lst1 and the first item of lst2, then the
result of applying g to
the first item of lst1 and the first item of lst2, then the
result of applying f to
the second item of lst1 and the second item of lst2, then the
result of applying g to
the second item of lst1 and the second item of lst2, etc. Test your
function on:
> (mapToThree + * '(1 2 3) '(4 5 6))
(5 4 7 10 9 18)
> (mapToThree cons list '(a b c) '(d e f))
((a . d) (a d) (b . e) (b e) (c . f) (c f))
> (mapToThree < > '(1 10 20) '(50 30 10))
(#t #f #t #f #f #t)
>
- Define a function compIt that takes three paramters, a function f
an item, x, and a list, ls. The parameter must be
some sort of test. compIt will use the test function, f, to
compare the item to each element of the list and will return a list of all items
that passed that test. Note that ls may itself contain lists that must
also be tested.
Test your function on:
- (compIt = 3 '(1 2 3 4 3 5) will return '(3 3)
- (compIt < 10 '(1 20 3 30 4 40) will return (20 30 40)
- (compIt eq? a '(a b c a d e a) will return (a a a)
- (compIt > 25 '(5 35 (7 12 33) 84 3 91) will return (5 (7 12) 3)
- Define two functions create-expression and results. The first
fucntion will take 4 parameters, two operators and two lists of numbers. You may
assume that the lists are well-formed (i.e., comprised of only numbers, no embedded
lists). create-expression must return an
expression that applys the second operator to each list and then applies the first
operator to those two results. For example, the call
The second function results will take an expression created by create-expression
and return the result of evaluating that expression.
Test your function on:
- (results (create-expression '+ '* '(1 2 3) '(4 5 6))) will return 126
- (results (create-expression '< '* '(1 2 3) '(4 5 6))) will return #t
- (results (create-expression '= '* '(1 2 3) '(4 5 6))) will return #f
- (results (create-expression '= '* '(1 2 3) '(1 2 3))) will return #t