Functional Problem Solving (CSC 151 2015S) : EBoards
Primary: [Front Door] [Schedule] - [Academic Honesty] [Disabilities] [Email] - [FAQ] [Teaching & Learning] [Grading] [Taking Notes] [Rubric] - [Calendar]
Current: [Assignment] [EBoard am] [EBoard pm] [Lab] [Outline] [Reading]
Sections: [Assignments] [EBoards am] [EBoards pm] [Labs] [Outlines] [Readings] - [Examples] [Handouts]
Reference: [Setup] [VM] [Errors] - [Functions A-Z] [Functions By Topic] - [Racket] [Scheme Report (R5RS)] [R6RS] [TSPL4]
Related Courses: [Davis (2013F)] [Rebelsky (2014F)] [Weinman (2014F)]
Misc: [Submit Questions] - [SamR] [Glimmer Labs] [CS@Grinnell] [Grinnell] - [Book Office Hours] - [Issue Tracker (Course)]
Overview
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
lengthprocedure 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.
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?
if is when you have one condition and you want to choose between
two options.cond is when you have multiple conditionswhen is for when you have one condition and no alternative if
the condition doesn't hold (that is, you don't want to do anything
if the condition doesn't hold)if, the options can only be a single expression; in when and
cond, the options can be multiple expressions, which are evaluated
in turn.Many Scheme programmers consider and and or additional mechanisms
for conditional evaluation. Why?
The reading was intended partially as review and partially to introduce new ideas. What new things did you learn about anonymous procedures?