Fundamentals of Computer Science I: Media Computing (CS151.02 2007F)
Primary: [Front Door] [Glance] - [Academic Honesty] [Instructions]
Current: [Outline] [EBoard] [Reading] [Lab] [Assignment]
Groupings: [Assignments] [EBoards] [Examples] [Exams] [Handouts] [Labs] [Outlines] [Projects] [Readings] [Reference]
Reference: [Scheme Report (R5RS)] [Scheme Reference] [DrScheme Manual]
Related Courses: [CSC151.01 2007F (Davis)] [CSC151 2007S (Rebelsky)] [CSCS151 2005S (Stone)]
Summary: In this laboratory, you will use and define higher-order procedures.
a. Create a new 200x200 image called canvas.
b. Add the list of rainbow colors to your definitions pane or to your library.
(define rainbow
(list color.red color.orange color.yellow color.green
color.blue color.indigo color.violet))
c. Read the following procedure and make sure that you understand what it is intended to do.
;;; Procedure:
;;; image.render-colors!
;;; Parameters:
;;; image, an image
;;; colors, a list of RGB colors
;;; Purpose:
;;; Render a list of colors in an image.
;;; Produces:
;;; [Nothing; Called for the side effect]
;;; Preconditions:
;;; colors is nonempty
;;; Postconditions:
;;; Each color in colors has been rendered somewhere on the image.
;;; Philosophy:
;;; Intended mostly as a technique for exploring color lists.
(define image.render-colors!
(lambda (image colors)
(let ((width (round (/ (image.width image) (length colors)))))
(letrec ((kernel
(lambda (left remaining)
(cond
((null? remaining)
(image.select-nothing! image)
image)
(else
(envt.set-fgcolor! (car remaining))
(image.select-rectangle! image selection.replace
left 0
; Select a bit too much to handle rounding effects
(+ 1 width) (image.height image))
(image.fill! image)
(kernel (+ left width) (cdr remaining)))))))
(kernel 0 colors)))))
d. Add image.render-colors! to your definitions pane
or to your library.
e. Add the definition of rgb.average to your definitions
pane or to your library.
;;; Procedure:
;;; rgb.average
;;; Parameters:
;;; color1, an RGB color
;;; color2, an RGB color
;;; Purpose:
;;; Averages color1 and color2
;;; Produces:
;;; average, an RGB color
;;; Preconditions:
;;; [None; called for the side effect]
;;; Postconditions:
;;; (rgb.red average) is the average of
;;; (rgb.red color1) and (rgb.red color2)
;;; (rgb.green average) is the average of
;;; (rgb.green color1) and (rgb.green color2)
;;; (rgb.blue average) is the average of
;;; (rgb.blue color1) and (rgb.blue color2)
(define rgb.average
(lambda (color1 color2)
(rgb.new (* 0.5 (+ (rgb.red color1) (rgb.red color2)))
(* 0.5 (+ (rgb.green color1) (rgb.green color2)))
(* 0.5 (+ (rgb.blue color1) (rgb.blue color2))))))
map
Recall that ( builds a new list by applying
map proc
lst)proc to each element of lst
in succession.
a. Use map to compute the successors to the squares of
the integers between 1 and 10. (That is, for each value in the list,
square it and then add 1.) Your result should be
the list (2 5 10 17 26 37 50 65 82 101).
b. Use map to take the last element of each list in a
list of lists. The result should be a list of the last elements.
For example,
>(map _____ (list (list 1 2 3) (list 4 5 6) (list 7 8 9 10) (list 11 12)))(3 6 10 12)
c.
Use
apply and map to sum the
last elements of each list in a list of lists of numbers. The result
should be a number.
>(map _____ (list (list 1 2 3) (list 4 5 6) (list 7 8 9 10) (list 11 12)))31 ; 3 + 6 + 10 + 12
Hint: You already know how to get a list of the last elements of each member list, so think about how to add them.
Reminder:
As you may recall,
( is another way to write
apply proc
lst)(
given that proc v1
v2 ... vn)lst
is (.
v1 v2
... vn)
a. What effect do you expect the following instruction to have?
>(image.render-colors! canvas (map rgb.complement rainbow))
b. Check your answer experimentally.
b. Write an expression to render a darker version of the rainbow on
canvas.
c. Write an expression to render a much darker version of the rainbow on
canvas. (For each color, call rgb.darker
three times.)
d. Here are three possible solutions to the previous problem. Which do you prefer? Why? Be prepared to discuss your reasons with the class.
>(image.render-colors! canvas (map rgb.darker (map rgb.darker (map rgb.darker rainbow))))
>(image.render-colors! canvas (map (lambda (c) (rgb.darker (rgb.darker (rgb.darker c)))) rainbow))
>(image.render-colors! canvas (map (compose rgb.darker (compose rgb.darker rgb.darker)) rainbow))
Although we often use the map procedure with only two
parameters (a procedure and a list), it can take more than two
parameters, as long as the first parameter is a procedure, the
remaining parameters are lists, and the procedure can legally take
the corresponding parameters from each list. For example, if the
procedure is +, each list must contain numbers.
a. What colors do you expect the following expression to produce?
>(map rgb.average rainbow (make-list (length rainbow) color.white))
b. Check your answer experimentally, by rendering the result on
canvas, by converting each color to a string, or both.
c. What do you expect the following expression to produce?
>(map rgb.average rainbow (make-list 5 color.white))
d. Check your answer experimentally.
e. What do you expect the following expression to produce?
>(map rgb.average rainbow (map rgb.complement rainbow))
f. Check your answer experimentally.
a. What do you think the value of the following expression will be?
>(map (lambda (x y) (+ x y)) (list 1 2 3) (list 4 5 6))
b. Check your answer through experimentation.
c. What do you think the value of the following expression will be?
>(map list (list 1 2 3) (list 4 5 6) (list 7 8 9))
d. Check your answer through experimentation.
e. What do you think Scheme will do when evaluating the following expression?
>(map (lambda (x y) (+ x y)) (list 1 2) (list 3 4) (list 5 6))
f. Check your answer through experimentation.
g. What do you think Scheme will do when evaluating the following expression?
>(map + (list 1 2 3) (list 4 5 6))
h. Check your answer through experimentation.
i. What do you think Scheme will do when evaluating the following expression?
>(map + (list 1 2) (list 3 4) (list 5 6))
j. Check your answer through experimentation.
Use apply and map to
concisely define a procedure, (, that takes
as arguments two lists of numbers, equal in length, and returns the
sum of the products of corresponding elements of the arguments:
dot-product
list1 list2)
>(dot-product (list 1 2 4 8) (list 11 5 7 3))73 ; ... because (1 x 11) + (2 x 5) + (4 x 7) + (8 x 3) = 11 + 10 + 28 + 24 = 73>(dot-product null null)0 ; ... because in this case there are no products to add
a. Document and write a procedure, (, that
counts the number of values in tally
predicate list)list for which
predicate holds.
b. Demonstrate the procedure by tallying the number of odd values in the list of the first twenty integers.
c. Demonstrate the procedure by tallying the number of multiples of three in the list of the first twenty integers.
Write a procedure, (filter , that creates a procedure
that takes a list as a parameter and removes all elements for which
predicate
list)predicate holds.
For example,
>(define filter-whitespace (lambda (lst) (filter char-whitespace? lst)))>(filter-whitespace (list #\a #\space #\b #\c))(#\a #\b #\c)>(list->string (filter-whitespace (string->list "Hello, my name is Dr. Fu")))"Hello,mynameisDr.Fu"
As you may recall, the left-section and
right-section procedures are defined as
(define left-section
(lambda (binproc arg1)
; Build a new procedure of one argument
(lambda (arg2)
; That calls binproc on the appropriate arguments
(binproc arg1 arg2))))
(define right-section
(lambda (binproc arg2)
; Build a new procedure of one argument
(lambda (arg1)
; That calls binproc on the appropriate arguments
(binproc arg1 arg2))))
a. Using right-section, define a procedure,
(,
that filters out all of the odd numbers from a list. Your definition
is likely to involve filter-odds lst)filter,
the odd? predicate, and
perhaps a few other things.
b. Write an expression (not a procedure), that filters out all
vowels from the string "The quick brown fox jumped over the lazy
dog." As a hint, you may find it helpful to use
string->list, list->string,
and perhaps even member.
Write a procedure, (
thoat hols only when any
pred? lst)lst contains at least
one value for which pred? holds.
Write a procedure, (, that replaces
each element of vector-map!
proc vec)vec with the result of applying
proc to the original element.
Primary: [Front Door] [Glance] - [Academic Honesty] [Instructions]
Current: [Outline] [EBoard] [Reading] [Lab] [Assignment]
Groupings: [Assignments] [EBoards] [Examples] [Exams] [Handouts] [Labs] [Outlines] [Projects] [Readings] [Reference]
Reference: [Scheme Report (R5RS)] [Scheme Reference] [DrScheme Manual]
Related Courses: [CSC151.01 2007F (Davis)] [CSC151 2007S (Rebelsky)] [CSCS151 2005S (Stone)]
Copyright © 2007 Janet Davis, Matthew Kluber, and Samuel A. Rebelsky. (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.