CSC151.01 2009F Functional Problem Solving : Labs
Primary: [Front Door] [Schedule] - [Academic Honesty] [Instructions]
Current: [Outline] [EBoard] [Reading] [Lab] - [Assignment]
Groupings: [Assignments] [EBoards] [Examples] [Exams] [Handouts] [Labs] [Outlines] [Projects] [Readings]
References: [A-Z] [By Topic] - [Scheme Report (R5RS)] [R6RS] [TSPL4]
Related Courses: [CSC151.02 2009F (Weinman)] [CSC151.02 2009S (Davis)] [CSC151 2008S (Rebelsky)]
Misc: [SamR] [MediaScript] [GIMP]
Summary: In this laboratory, you will explore the use of a variety of forms of anonymous procedures. Our primary focus will be on the use of anonymous procedures and the “drawings as values” model to visualize a variety of functions (the mathematical kind).
Make a copy of anonymous-procedures-lab.scm
,
which contains some useful code for this lab.
In the code for this lab, there is a value named points
.
a. Explain what that value contains.
b. Check your answer experimentally by rendering that value.
>
(check-drawings points)
In this laboratory, we're going to explore ways to visualize functions
by systematically shifting the values in points
. Hence,
most of our commands will look something like:
>
(check-drawings (map drawing-vshift points (map function numbers)))
Use this form to visualize the functions increment
,
sqrt
, and square
. You should visualize the
three functions separately.
You may notice that the function is “upside-down”, because the GIMP coordinate system is opposite the Cartesian coordinate system. We won't worry about this switch in orientation.
Here's our traditional definition of a function that multiplies its parameter by 5. (We've included a bit more documentation than normal.)
;;; Procedure: ;;; times5 ;;; Parameters: ;;; x, a number ;;; Purpose: ;;; Multiply x by 5 ;;; Produces: ;;; result, a number ;;; Preconditions: ;;; If x is an inexact number, it is no more than one-fifth the highest ;;; inexact number and no less than one-fifth the highest inexact ;;; number. ;;; Postconditions: ;;; result = 5*x, within the precision. ;;; result is the same "type" as x. For example, ;;; If x is exact, result is exact. ;;; If x is inexact, result is inexact. ;;; If x is an integer, result is an integer. ;;; If x is complex, result is complex. (define times5 (lambda (x) (* 5 x)))
a. Using the form from the previous exercise, visualize this function.
b. As we noted in the reading, instead of writing times5
for the function, we can write a lambda expression. Fill in the rest of the following to get the same effect as we got in the previous step.
>
(check-drawings (map drawing-vshift points (map (lambda (x) ______) numbers)))
c. Write a similar expression to visualize the function “multiply x by -3”.
d. Write a similar expression to visualize the function “subtract ten from x”.
e. Write a similar expression to visualize the remainder you get when dividing x by 10.
After completing this exercise, you may want to compare your answers to those in the notes.
These lambda expressions are a bit long. As you may recall from
the
reading we can also create simple procedures with left-section
(l-s
) and right-section (r-s
).
a. Predict the results of the following expressions.
> (map (l-s + 10) (iota 20)) > (map (r-s + 10) (iota 20)) > (map (l-s - 10) (iota 20)) > (map (r-s - 10) (iota 20)) > (map (l-s * 10) (iota 20)) > (map (r-s * 10) (iota 20)) > (map (l-s / 10) (iota 20)) > (map (r-s / 10) (iota 20)) > (map (r-s mod 10) (iota 20))
b. Check your answers experimentally. If any of the results fail to make sense, please discuss them. (You might talk to a classmate, the class mentor, your teacher, or some combination thereof.)
c. Redo parts b, c, d, and e from the previous exercise using sections instead of the lambda expressions. If you have difficulty, you may want to check the notes on this exercise.
d. Go back to the definition of points
and write an
expression to space them out a bit horizontally, multiplying the
values in numbers
by 3 before shifting.
In each of these cases, we've graphed a fairly simple function. What if we want to do something a bit more complex. For example, what if, after computing the square root, we want to “stretch out” the visualization by multiplying by 5? What if we want to first subtract 25 (putting the origin closer to the middle of the screen) and then multiply by 5? What if we want to do some other weird combination of procedures?
The composition procedure, o
, described in
the
reading, helps with all of these problems and more.
As you may recall, (o f g)
is a function that
applies g
and then applies f
.
a. Predict and check the results of the following expressions. You may want to check your prediction for each one before going on to the next.
>
(map (o increment square) (iota 20))
>
(map (o square increment) (iota 20))
>
(map (o increment (l-s * 2)) (iota 20))
>
(map (o (l-s * 2) increment) (iota 20))
>
(map (o (l-s + 10) (l-s * 2)) (iota 20))
>
(map (o (l-s * 2) (r-s mod 5)) (iota 20))
b. The square root visualiation is somewhat shallow. Let's stretch it out a bit. Write an expression to visualize the function “compute the square root and then multiply by 10”.
c. In contrast, the visualization of the square function is perhaps stretched out too much. Write an expression to visualize the function “square and then divide by 100”.
d. Write an expression to visualize the function “compute the remainder after dividing by 11, and then multiply that remainder by 7”.
e. Write an expression to visualize the function “multiply by five and then compute the remainder after dividing by 11”.
f. Write an expression to visualize the function “divide by 10,
compute the sine (with sin
), and then multiply the result
by 100”.
If you happen to finish early, experiment with various compositions and sections of functions. For example, you might try to get a “stairstep” graph by dividing and then rounding.
Here's a simple framework
(define num-points 50) (define numbers (iota num-points)) (define make-color (lambda (n) (rgb_new (___ n) (___ n) (___ n)))) (define basic-drawing (drawing-group drawing-unit-circle (drawing-vshift drawing-unit-square 0.5))) (define my-drawing-01 (map drawing-recolor (map drawing-hshift (map drawing-vshift (map drawing-hscale (map drawing-vscale (make-list num-points basic-drawing) (map ___ numbers)) ; vscale (map ____ numbers)) ; hscale (map ___ numbers)) ; vshift (map ___ numbers)) ; hshift (map (o make-color ___) numbers))) ; color
Here are our solutions to the problems.
b.
>
(check-drawings (map drawing-vshift points (map (lambda (x) (* 5 x)) numbers)))
c.
>
(check-drawings (map drawing-vshift points (map (lambda (x) (* -3 x)) numbers)))
d.
>
(check-drawings (map drawing-vshift points (map (lambda (x) (- x 10)) numbers)))
e.
>
(check-drawings (map drawing-vshift points (map (lambda (x) (remainder x 10)) numbers)))
For part c, we expect something like the following:
>
(check-drawings (map drawing-vshift points (map (l-s * 5) numbers)))
>
(check-drawings (map drawing-vshift points (map (l-s * -3) numbers)))
>
(check-drawings (map drawing-vshift points (map (r-s - 10) numbers)))
>
(check-drawings (map drawing-vshift points (map (r-s remainder 10) numbers)))
For the last of the three, it would also be reasonable to write
>
(check-drawings (map drawing-vshift points (map (r-s mod 10) numbers)))
Primary: [Front Door] [Schedule] - [Academic Honesty] [Instructions]
Current: [Outline] [EBoard] [Reading] [Lab] - [Assignment]
Groupings: [Assignments] [EBoards] [Examples] [Exams] [Handouts] [Labs] [Outlines] [Projects] [Readings]
References: [A-Z] [By Topic] - [Scheme Report (R5RS)] [R6RS] [TSPL4]
Related Courses: [CSC151.02 2009F (Weinman)] [CSC151.02 2009S (Davis)] [CSC151 2008S (Rebelsky)]
Misc: [SamR] [MediaScript] [GIMP]
Copyright (c) 2007-9 Janet Davis, Matthew Kluber, Samuel A. Rebelsky, and Jerod Weinman. (Selected materials copyright by John David Stone and Henry Walker and used by permission.)
This material is based upon work partially supported by the National Science Foundation under Grant No. CCLI-0633090. Any opinions, findings, and conclusions or recommendations expressed in this material are those of the author(s) and do not necessarily reflect the views of the National Science Foundation.
This work is licensed under a Creative Commons
Attribution-NonCommercial 2.5 License. To view a copy of this
license, visit http://creativecommons.org/licenses/by-nc/2.5/
or send a letter to Creative Commons, 543 Howard Street, 5th Floor,
San Francisco, California, 94105, USA.