Functional Problem Solving (CSC 151 2014S) : EBoards

CSC151.02 2014S, Class 08: Documenting Programs and Procedures


Overview

Preliminaries

Admin

Upcoming Work

Questions

What does a procedure definition look like?

    (define PROC
      (lambda (PARAMS)
        EXPRESSION))

For example.

    (define munge
      (lambda (x)
        (+ x (* x x))))

    (define average
      (lambda (x y)
        (/ (+ x y) 2)))

How can we think about a procedure call?

Approach 1: Replacement. In the body of the procedure, replace every copy of the parameter with the corresponding value in the call.

    (munge 5) => (+ 5 (* 5 5))
    (munge 7) => (+ 7 (* 7 7))
    (munge (- 2 5)) => (munge -3) => (+ -3 (* -3 -3))
    (average 3 4) => (/ (+ 3 4) 2)

Approach 2: Implicit defines. Implicitly add a define statement for each parameter using the corresponding value in the call, then evaluate the body, then forget the definition.

    (munge 5) => (define x 5) 
                 (+ x (* x x))
                 [forget the definition]
    (munge 7) => (define x 7) 
                 (+ x (* x x))
                 [forget the definition]
    (average 3 4) => (define x 3) 
                     (define y 4) 
                     (/ (+ x y) 2)
                     [forget the definitions]

Reality is closer to the second than the first, but it doesn't really matter.

Can you explain the relationship between shifting and scaling?

Behind the scenes, we represent the basic drawings with a few values type, color, left, top, width, height

When we hshift a drawing, we add to the left

When we vshift a drawing, we add to the top

When we hscale a drawing, we multiply the width AND we multiply the left

When we vscale a drawing, we multiply the height AND we multiply the top

When we scale a drawing, we multiply width AND height AND left AND top

What happens to the center? Is it also scaled?

The x coordinate of the center is (+ left (/ width 2))

The x coodinate of the scaled cenetr is (+ (* scale left) (/ (* scale width) 2))

The x coodinate of the scaled cenetr is (+ (* scale left) (* scale (/ width) 2))

The x coodinate of the scaled cenetr is (* scale (+ left (/ width) 2))

Conclusion: Yes.

What if I want to scale the object and keep the top and the left the same?

Option 1: Scale it, figure out how much it moved, move it back

Option 2: Write our own procedure that takes advantage of the underlying representation and just scale width and height.

Option 3: Move it so that the top is 0 and the left is 0. Then scale, then move it back

Quiz!

STAY IN YOUR SEATS. YOUR MENTORS WILL HELP COLLECT THE EXAMS!

If you finish early, think about how you would document a neighbor procedure that creates a copy of a drawing immediately to the right of the original drawing.

Practice


Samuel A. Rebelsky, rebelsky@grinnell.edu

Copyright (c) 2007-2014 Janet Davis, Samuel A. Rebelsky, and Jerod Weinman. (Selected materials are copyright by John David Stone or Henry Walker and are used with permission.)

Creative Commons License

This work is licensed under a Creative Commons Attribution 3.0 Unported License. To view a copy of this license, visit http://creativecommons.org/licenses/by-nc/3.0/ or send a letter to Creative Commons, 543 Howard Street, 5th Floor, San Francisco, California, 94105, USA.