Functional Problem Solving (CSC 151 2016S) : EBoards
Primary: [Front Door] [Schedule] - [Academic Honesty] [Disabilities] [Email] - [FAQ] [Teaching & Learning] [Grading] [Taking Notes] [Rubric]
Current: [Assignment] [EBoard] [Lab] [Outline] [Reading]
Sections: [Assignments] [EBoards] [Labs] [Outlines] [Readings] - [Examples] [Handouts]
Reference: [Setup] [Remote] [VM] [Errors] - [Functions A-Z] [Functions By Topic] - [Racket] [Scheme Report (R5RS)] [R6RS] [TSPL4]
Related Courses: [Curtsinger (2016S)] [Davis (2013F)] [Rebelsky (2015F)] [Weinman (2014F)]
Misc: [SamR] [Glimmer Labs] [CS@Grinnell] [Grinnell] - [Issue Tracker (Course)]
MathLAN is dead! It will return some day (hopefully today)
Overview
Questions
At the mentor sessions, can we ask general questions related to the test? (E.g., "Can we work through this lab problem that is incredibly closely related to the exam?")
Yes.
(define f (lambda (q) (floor (/ (irgb-red q) (max (irgb-red q) (irgb-green q) (irgb-blue q))))))
Explanation: f is a procedure that takes in q, which is a sandwich
or something, it takes the red value of q and then defines it by
some snappy maximum.
This gives you 1 if the red component is the largest component and 0 if the red component is not the largest component.
When we write procedures, it's useful to write documentation that explains what our procedures do.
Three audiences:
In this class, when you write procedures, you also document those procedures. (Primarily focusing on those who will use your code, "clients").
We document with words that begin with the letter P (six of them, to be precise)
;;; Procedure:
;;; red-dominates
;;; Parameters:
;;; color, an integer-encoded RGB color
;;; Purpose:
;;; Determine whether the red component is the largest
;;; of the three components.
;;; Produces:
;;; dominates, an integer
;;; Preconditions:
;;; [No additional]
;;; Postconditions:
;;; If any other component is greater than the red component,
;;; returns 0
;;; Otherwise
;;; returns 1
Preconditions and postconditions are attempts to formalize expectations about the parameters.
vs. ;;; Parameters: ;;; color, a color ;;; Preconditions: ;;; color must be in integer-encoded RGB form
Typical snarky student question: "If both the red and the blue component are 200 and the green component is 100, is the red component really the largest?"
Additional P's
;;; Problems:
;;; Doesn't work when every component is 0.
Could fix the problem with
;;; Precondition:
;;; color is not black.
P's, continued
;;; Practica:
;;; (red-dominates (irgb 200 100 0)) -> 1
;;; (red-dominates (irgb 0 100 200)) -> 0
;;; (red-dominates (irgb 200 200 100)) -> 1
;;; Props:
;;; Based on code written by Sam Rebelsky on 2016-02-10.
;;; Process:
;;; Divide the red component by the largest component
;;; and do some tricky math.
;;; Philosophy:
;;; Useful in procedures that do different things depending
;;; on which component dominates.
Sketch the six P's for this procedure
;;; Procedure: ;;; red-blue-plus-32 ;;; Parameters: ;;; color, a color ;;; Purpose: ;;; Makes color more purple. ;;; Produces: ;;; purpler, a color ;;; Preconditions: ;;; color must be an integer-encoded IRGB color ;;; Postconditions: ;;; purpler is an integer-encoded RGB color. ;;; The red component is larger by 32, if possible ;;; If (irgb-red color) <= 223, ;;; (irgb-red purpler) = (+ 32 (irgb-red color)) ;;; If (irgb-red color) > 223, (irgb-red purpler) = 255 ;;; The green component is the same ;;; (irgb-green purpler) = (irgb-green color) ;;; The blue component is larger by 32, if possible ;;; If (irgb-blue color) <= 223, ;;; (irgb-blue purpler) = (+ 32 (irgb-blue color)) ;;; If (irgb-blue color) > 223, (irgb-blue purpler) = 255 (define g (section irgb-add <> (irgb 32 0 32)))
red-dominates fails to work when the color is (irgb 0 0 0),
because then we are dividing by 0.
(define red-dominates (lambda (color) (floor (/ (+ 1 (irgb-red color)) (+ 1 (max (irgb-red color) (irgb-green color) (irgb-blue color)))))))
How do we use red-dominates?