Functional Problem Solving (CSC 151 2015S) : EBoards

CSC151.01 2015S, Class 20: Anonymous Procedures, Revisited


Overview

Preliminaries

Admin

Upcoming Work

Extra Credit Opportunities

Academic

Peer Support (Morning Section)

Other Good Things

Questions

How do I copy and paste from images?

;;; Procedure:
;;;   image-copy-paste-block!
;;; Parameters:
;;;   source, an image id
;;;   source-col, an integer
;;;   source-row, an integer
;;;   target, an image id
;;;   target-col, an integer
;;;   target-row, an integer
;;;   width, an integer
;;;   height, an integer
;;; Purpose:
;;;   Copy a width-by-height block from source to target, with the 
;;;   top-left corner of each block as specified.
;;; Produces:
;;;   [Nothing; called for the side effect.]
;;; Problems:
;;;   Does not do well with improper column/row pairs (e.g., those
;;;   outside the bounds of the image.

I'm having trouble with problem 2. Any hints?

Start by making sure you understand the problem, perhaps by working things through by hand.

(visualize-transforms 
    (color->irgb "pink")
    (list (lambda (irgb-color) irgb-color)
          irgb-darker 
          (o irgb-darker irgb-darker)
          (o irgb-darker irgb-darker irgb-darker)
          (o irgb-darker irgb-darker irgb-darker irgb-darker))
     5)

There are three parameters. The first is a color. (Is it an integer-encoded RGB color, a color name, or a list of the red/green/blue components or ...?) Integer-encoded IRGB color. Second is a list of transformations. What's a "transformation"? It's a function from irgb values to irgb-values. The 5 is the number of transformations you have. (It's used as a parameter to visualize-colors, so we include it here for convenience. The length procedure would also allow us to compute it.)

Goal (for this call): Apply each of these five transformations to the pink color, make a shape in each color, and show the five shapes.

> (define c (color->irgb "pink"))
> c
16761035
> (define t1 (lambda (irgb-color) irgb-color))
> (t1 c)
16761035
> ((lambda (irgb-color) irgb-color) c)
16761035
> (define t2 irgb-darker)
> (t2 c)
15708347

Now we are ready to put them in a list and then show them

    > (visualize-colors (list 16761035 15708347 ...) 5)

Decompose problems. You want to make a list of colors, and then use visualize-colors.

How do you make the list of colors? Well, given a list of transformations of the form '(t1 t2 t3 t4 t5), you want to make the list that contains the values (t1 irgb-color), (t2 irgb-color), (t3 irgb-color), (t4 irgb-color), and (t5 irgb-color).

Given one list, you make another list with an appropriate call to map. Your challenge is to write that call.

How do you write post-conditions for a procedure that returns a procedure?

Option 1:

;;; Produces:
;;;   proc, a procedure
;;; ...
;;; Postconditions:
;;;   proc has the form (proc param1 param2) where param1 is a
;;;   an integer and param2 is an image id.  proc computes ...

Option 2:

;;; Produces:
;;;   proc, a procedure from integers and image ids to image ids.
;;; ...
;;; Postconditions:
;;;   (proc param1 param2) has the value ...

Option 3:

;;; Produces:
;;;   proc, a procedure
;;; ...
;;; Postconditions:
;;;   proc meets the following specifications
;;;     Parameters:
;;;       param1, an integer
;;;       param2, an image id
;;;     Purpose:
;;;       ...
;;;     Produces:
;;;       image, an image id
;;;     Preconditions:
;;;       [No additional]
;;;     Postconditions:
;;;        ...

In Problem 4 on Assignment 5, does "a simple shape of your choice" mean that the user can customize a shape, or that the programmer decides what shape to use?

I was assuming that you, in implementing the procedure, would decide what the shape would be.

Conditionals, Revisited

You learned three new mechanisms for conditional evaluation: if, when, and cond. What are the similarities and differences between them? Why might you use one over another?

Many Scheme programmers consider and and or additional mechanisms for conditional evaluation. Why?

Anonymous Procedures, Revisited

The reading was intended partially as review and partially to introduce new ideas. What new things did you learn about anonymous procedures?