Functional Problem Solving (CSC 151 2014F) : EBoards

CSC 151: Extra Session 3 (Thursday, 18 Sept. 2014)


What are the potential topics for tomorow's quiz?

Can we talk about procedures with side effects?

> (define x 5)
> (define y (square x))
> y
25
> x
5

> (define d (scale-drawing 10 drawing-unit-circle))
> (define d2 (hshift-drawing 20 d))
> (drawing-left d2)
15
> (drawing-left d)
-5
> (define kitten (image-load "/home/rebelsky/Desktop/kitten.jpg"))
> kitten
1
> (image-get-pixel kitten 0 0)
3037011
> (irgb->string (image-get-pixel kitten 0 0))
"46/87/83"
> (image-set-pixel! kitten 0 0 "yellow")
> (irgb->string (image-get-pixel kitten 0 0))
"255/255/0"
> (image-show kitten)
1
        > (define silly
        (lambda (name)
          (display "Hello ")
          (display name)
          (display ".")
          (newline)
          5))
    > (silly "Sam")
    Hello Sam.
    5
* Note: We see output *and* we get a value back.

* We say that the second and third models have "side effects". * What about that !? * Custom is to use the ! when you write a procedure with side effects. * But custom is not always followed. E.g., display doesn't have a !. * And then there's image-show. * It doesn't change the pixels. * But it changes the state, perhaps dangerously.

What's the relationship between image-show and drawing-render!?

What about drawing->image?

Can you talk to us about check and test and the rest?

    > (define prof "SamR")
    > (define sam (string-append "Sam" "R"))
    > prof
    "SamR"
    > sam
    "SamR"
    > (require rackunit)
    > (check-= prof sam 0)
    --------------------
    ERROR
    . . -: contract violation
      expected: number?
      given: "SamR"
      argument position: 1st
      other arguments...:
       "SamR"

    --------------------
    > (check-eq? prof sam 0)
    --------------------
    FAILURE
    actual:     "SamR"
    expected:   "SamR"
    name:       check-eq?
    location:   (unsaved-editor331 43 2 840 22)
    expression: (check-eq? prof sam)
    message:    0

    . . Check failure
    --------------------
    > (check-equal? prof sam 0)
 (define sqrt-tests
   (test-suite
     "Tests of the square root method"
     (test-case "If you square the square root (or vv), you get the original"
                (check-= (sqrt (square 2)) 2 0.000001)
                (check-= (square (sqrt 2)) 2 0.000001))
     (test-case "Easy test: sqrt 4.  If this doesn't work, things are broken."
                (check-= (sqrt 4) 2 0))
     (test-case "When sqrt can have an exact result, it should"
                (check-equal? (exact? (sqrt 4)) #t))
     (test-case "Check zero"
                (check-= (sqrt 0) 0 0))
     (test-case "Imaginary sqrt(-1)"
                (check-= (sqrt -1) 0+i 0.0000001))))

 (define ss-test
   (lambda (num)
     (test-suite
       "Checking square roots"
        (check-= (sqrt (square num)) num 0.000001)
        (check-= (square (sqrt num)) num 0.000001)
        (check-= (square (square (sqrt (sqrt num)))) num 0.0000001))))

 (define sqrt-tests
   (test-suite
     "Tests of the square root method"
     (test-suite
       "Roots of 2"
       (ss-test 2))
     (test-suite
       "Roots of 9273"
       (ss-test 9273))

Given documentation for a procedure, how do you write the procedure?

 ;;; Procedure:
 ;;;   whatever
 ;;; Parameters:
 ;;;   alpha, a number
 ;;;   beta, another number
 ;;;   image, an image
 ;;; Purpose:
 ;;;   Put a blue ellipse on image.
 ;;; ...
 ;;; Postconditions:
 ;;;   The width of the ellipse is alpha
 ;;;   The height of the ellipse is beta
 ...
        (define whatever
          (lambda (alpha beta image)
            ...))

Will you send me my responses to the prologue?