...
- Based upon the preceding equation (
1
, what should the following evaluate to? Think about them first, then try them in DrScheme (wherefoldr
is pre-defined).- (foldr + 5 (list -1 5 -3 4 2))
- (foldl + 5 (list -1 5 -3 4 2))
- (foldr - 5 (list -1 5 -3 4 2))
- (foldl - 5 (list -1 5 -3 4 2))
- (foldr cons empty (list -1 5 -3 4 2))
- (foldl cons empty (list -1 5 -3 4 2))
- (foldr append empty (list (list 1 2) (list 4) empty (list 8 1 2)))
- (foldr append empty (list (list 1 2) (list 4) empty (list 8 1 2)))
- What is the contract for
foldr
? Forfoldl
? You should be able to determine this from the equations and the examples above.
(We also covered the typing offoldr
in lecture.) Hint: First, determine the type offoldr
andfoldl
assuming the input list is a list of numbers, and then generalize. - Using
foldr
, define a function to compute the product of a list of numbers. Do the same forfoldl
. - Using
foldr
, definemap
. (Also done in lecture.) Do the smae same forfoldl
. - Define the function
Foldr
to satisfy equation (1) above. As you might expect, it follows the template for a function consuming a list. (It was also done in lecture.) Test your function against Scheme's built-infoldr
to make sure they give the same results for the same inputs. - Define the function
Foldl
to satisfy equation (2) above. As you might expect, it follows the template for a function consuming a list. Test your function against Scheme's built-infoldl
to make sure they give the same results for the same inputs. - Define a function to compute whether all elements in a list of numbers are greater than 6. Write two version versions, one using
foldr
and one usingfoldl
, choosing suitable names (distinct fromfilter
for each.
Then generalize both definitions to definefilter
. Are your twofilter
functions identical? Hint: look at computations that generate errors. - Define a function that, given a list of numbers, returns the sum of all the positive numbers in the list. Write two versions, one using
foldr
and one usingfoldl
. - Without using explicit recursion, develop a function
upfrom
that, giveni
andn
returns the list of lengthi
of numbers up ton
.
...