Functional Problem Solving (CSC 151 2014F) : EBoards

CSC151.01 2014F, Class 18: Programming the GIMP Tools


Overview

Preliminaries

Admin

Upcoming Work

Fun Things with no Extra Credit Value

Extra Credit Opportunities

Academic

Peer Support

Miscellaneous

Questions

Not-so-quick Notes on Lambda Expressions

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)))))))

Questions about the readings

Lab