...
A local
expression introduces a new local scope--the names in the definitions are visible both within the bodies of the definitions and within the body. If a local name collides with (matches) a name in the surrounding program, the local name shadows the enclosing name; i.e., the matching name in the enclosing program is invisible within the local
expression; only the matching local name can be accessed. The same shadowing phenomenon happens when a parameter name in a function definition collides with a name defined in the surrounding program.
{{Wiki Markup (local
\[
}} definitions {{...
\]
}} body {{)
}}
...
Note that the use of square brackets \ [ \ ] here is equivalent to parentheses( ). The brackets help set off the definitions a little more clearly for readability.
In order to use local and the other features about to be introduced in class, you need to set the DrScheme language level to Intermediate Student.
Exercises
Finding the maximum element of a list.
Let's consider the problem of finding the maximum number in a list which is used an example in Lecture 7.
...
In general, you can take any program using local
, and turn it into an equivalent program without local
. Using local
doesn't let us write programs which were impossible before, but it does let us write them more cleanly and concisely.
Generating lists of ascending numbers
Retrieve your code for the up
function and its helper upfrom
from last lab.
...
- Load one of the more interesing Scheme programs you have written (such as HW2) into DrScheme. (If you did the preceding optional exercise, you can use this program.) Perform the "Check Syntax" command to identify where each variable is bound. Recall that when you put your cursor above a binding instance, it will draw arrows to the uses, and when you put your cursor above a use, it will draw an arrow to its binding instance.
- X Using the Scheme library function
filter
, develop a function that takes a list of numbers and returns a list of all the positive numbers in that list.
...
These functions are very similar and can be written trivially using the Scheme library function map
discussed in Lecture 9.
Exercises.
- Write
double-nums and <3-nums
usingmap
.
The Big Picture
...
Many functions we've written fit this pattern, although this fact might not be obvious at first.
Exercises
<span style="color: #ff0000">X</span> Based upon the preceding equation (\[1\X Based upon the preceding equation ([1]), what should the following evaluate to? Think about them first, then try them in DrScheme (where {{Wiki Markup foldr
}} 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)))
- X 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 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
.
...
The following can be defined using some combination of the pre-defined abstract functions.
- X Define a function that, given a list of numbers, determines whether all of the numbers in the list are even. Write two versions one using
foldr
and one usingfoldl
. - Define
andmap
andormap
usingfoldr
rather than recursion. Do the same usingfoldl
.
...