Name _________________________
CS 60 Worksheet Number 3
Fall 1997
High-Level Functional Programming
This worksheet is keyed to the second part of Chapter 3 of the notes. This worksheet is worth 10 points if turned in by class time Wed. 2/4/98.
Instructions: Write your answer on the line. Anything you write after the discussion in class is to be written below the line.
1. What is a function?:
________________________________________________________.
2. Give an example of a function on the domain of lists:
________________________________________________________.
3. What is an example of a partial function?
________________________________________________________.
4. If the list [3, 5, 7, 11, 13] were to be considered a partial function, call it L, what would L(3) be? L(0)? L(5)?
________________________________________________________.
5. If we to describe the list L above as L: A B, what would be possible choices for A and B?
________________________________________________________.
6. What are two principal ways to represent a function for computational purposes?
________________________________________________________.
7. Enumerate the function "remainder after dividing by 3" on the domain {0, 1, 2, 3, 4, 5, 6}?
________________________________________________________.
8. The number of arguments a function expects is called its _________?
9. What is the value of prefix(3, scale(5, range(1, 10)))?
________________________________________________________.
10. In what way does a function modify its arguments?
________________________________________________________.
11. What function could you use to determine whether a specific element occurs in a list?
________________________________________________________.
12. Using the rex function assoc, what is the value of
assoc("april", [["jan", 31], ["feb", 28], ["mar", 31],
["april", 30], ["may", 31]])?
________________________________________________________.
13. Give a one-line rex definition of the function infix(I, J, L) which yields elements I through J of list L, where the first element is counted as element 0. For example,
rex > infix(1, 5, range(0, 10));
[1, 2, 3, 4, 5]
________________________________________________________.
14. What is meant by mapping a function over a list?
________________________________________________________.
15. If f is the function "remainder after dividing by 3", what is the value of map(f, [3, 4, 5, 6, 7])?
________________________________________________________.
16. Articulate the meaning of the expression (Z) => Z*Z+1.
________________________________________________________.
17. What is the value of map((Z) => Z*Z+1, [3, 4, 5, 6, 7])?
________________________________________________________.
18. What is the value of ((Z) => Z*Z+1)(9)?
________________________________________________________.
19. Suppose that times3 is the function which multiplies a number by 3. What is the value of ((F) => F(5))(times3)?
________________________________________________________.
20. For times3 as above, what is the value of ((F)=>F(F(5)))(times3)?
________________________________________________________.
21. Articulate the following: (F) => (X) => F(F(X)).
________________________________________________________.
22. Write the expression which will pair up each value of a list with the number 3? with the value of a variable x.
________________________________________________________.
23. Using 22, write the expression which will create a list of all possible pairs of two lists L and M. Then define a function which will do this for any two list arguments L and M.
________________________________________________________.
24. Which functional identities are correct?
map(F, reverse(L)) == reverse(map(F, L))
map(F, append(L, M)) == append(map(F, L), map(F, M))
________________________________________________________.
25. What is a predicate?
________________________________________________________.
26. Give a rex expression which defines the predicate "is divisible by 5".
________________________________________________________.
27. Suppose P is bound to the predicate "is divisible by 5" and L is bound to the list [3, 5, 7, 11, 15, 19]. What are the values of the following:
some(P, L)_______________________________________________
all(P, L)_______________________________________________
keep(P, L)_______________________________________________
drop(P, L)_______________________________________________
find(P, L)_______________________________________________
find_indices(P, L) ________________________________________
28. Suppose we have a list of pairs of numbers and we wish to create a list of these numbers in which they ordered by increasing sum. How would we use the 2-argument sort function to do this?
________________________________________________________.
29. How would we use the 2-argument function max which finds the maximum of its 2 arguments to find the maximum of an arbitrary list L of positive numbers?
________________________________________________________.