Functional Problem Solving (CSC 151 2014F) : EBoards
Primary: [Front Door] [Schedule] - [Academic Honesty] [Disabilities] [Email] - [FAQ] [Teaching & Learning] [Grading] [Rubric] - [Calendar]
Current: [Assignment] [EBoard] [Lab] [Outline] [Reading]
Sections: [Assignments] [EBoards] [Examples] [Handouts] [Labs] [Outlines] [Readings]
Reference: [Setup] [VM] [Errors] - [Functions A-Z] [Functions By Topic] - [Racket] [Scheme Report (R5RS)] [R6RS] [TSPL4]
Related Courses: [Davis (2013F)] [Rebelsky (2014S)] [Weinman (2014F)]
Misc: [Submit Questions] - [SamR] [Glimmer Labs] [CS@Grinnell] [Grinnell] - [Issue Tracker (Course)]
Overview
A problem related to the quiz. Given an image, picture, modify the picture to increase all of the red components by 20%, set the green to 0.
(image-transform! picture TRANSFORMATION)
(define yeahidunno
(lambda (color)
(irgb (bound-component (* 1.2 (irgb-red color)))
0
(irgb-blue color))))
; Assume (bound-component val) bounds val to the range 0 .. 255.
(image-transform! picture yeahidunno)
In Scheme, you can always substitute a value for its name.
(image-transform!
picture
(lambda (color)
(irgb (bound-component (* 1.2 (irgb-red color)))
0
(irgb-blue color))))
If we use this strategy, we don't need to come up with a name for the procedure (but we could if forced to).
In this problem, we didn't need the internal lambda, but it helped. In other problems, we may need an iinternal lambda.
A variant of the previous problem: Write a procedure,
(enhance-blue! image percent), increase blue component by that percent.
We'll assume that percentages are reprsented as decimals. E.g., 25%
is 0.25.
(image-transform! picture TRANSFORMATION)
(define color-enhance-blue
(lambda (color)
(irgb (irgb-red color)
(irgb-green color)
(* (+ 1 percent) (irgb-blue color)))))
(define enhance-blue!
(lambda (image percent)
(image-transform! picture color-enhance-blue)))
Whoops! Doesn't work. Let's try the substitution anyway
(define enhance-blue!
(lambda (image percent)
(image-transform!
picture
(lambda (color)
(irgb (irgb-red color)
(irgb-green color)
(* (+ 1 percent) (irgb-blue color)))))))