---
title: Eboard 32  Analyzing procedures
number: 32
section: eboards
held: 2017-11-13
---
CSC 151.03, Class 32:  Analyzing procedures
===========================================

_Overview_

* Preliminaries
    * Notes and news
    * Upcoming work
    * Extra credit
    * Questions
* Lab
* Debrief

### News / Etc.

* We finally get to analyzing procedures today.  Sorry for the delay.
* Please try to keep the room straight.  
* Exam 2 returned last night.  If you didn't get it, let me know.
* Quiz 10 and 11 returned.  Some issues on `l-s` vs. `r-s`

### Upcoming Work

* Reading for Wednesday: [Association lists](../readings/association-lists)
    * Since it was ready for today, it's still ready for Wednesday.
* No writeup for class 31.
* [Writeup for class 32](../writeups/writeup32) due Wednesday at 10:30 p.m.
    * Exercise 6
    * To: <csc151-03-grader@grinnell.edu>
    * Subject: CSC 151.03 Writeup 32 (YOUR NAMES)
* [Exam 3](../exams/exam03) 
    * Exam due TOMORROW
    * Cover pages due Wednesday the 15th.
    * Epilogues due Wednesday the 15th.

### Extra credit (Academic/Artistic)

* Convocation Thursday at 11 a.m. in JRC 101.  "Work's Provocative Future: 
  Which Graduates Will Thrive?"

### Extra credit (Peer)

* "The First Time I Walked on the Moon".  Thursday at 7:30 and Friday
  at 7:30 and Saturday at 7:30 and Sunday at 2:00.  In the Flanagan.

### Extra credit (Misc)

### Other good things

* Fresh Flutes Thursday
* Voice recitals Friday at 4:15 (Henderson) and 7:00 (Manuel)
* Orchestra Saturday at 2pm
* Women's Basketball vs. Emmaus Wed at 5:00 p.m.
* Men's Basketball vs. Emmaus Wed at 7:00 p.m.
* Swimming and Diving Saturday at 1:00 p.m.

### Questions

Could you help me understand the difference between left-section and right-section?
  : Sure.
  : We want to fill in one parameter of a two-parameter procedure.  That
    leaves you with a one-parameter procedure.
  : Left-section fills in the left parameter, right section fills in
    the right parameter.
  : `(l-s proc val)` -> `(section proc val <>)`
  : `(r-s proc val)` -> `(section proc <> val)`
  : "A procedure that divides by 2" (/ ___ 2).  `(r-s / 2)`.
  : "A procedure that divides by n" `(r-s / n)`.

I didn't get the tree recursion problem on the quiz.  I think understanding it might be helpful.  Can we go over it?
  : Sure.
  : Thought process: A value is in a tree if (1) the tree is not empty
    AND (2) either (a) the value is at the root, (b) the value is in
    the left subtree, or (c) the value is in the right subtree.
  : English puts "and" and "or" in the middle.  Scheme puts them on
    the outside.

```
(define tree-contains?
  (lambda (tree val)
    (and (not (empty? tree)) 
         (or (equal? val (contents tree))
             ; TTMRF
             (tree-contains? (left tree) val)
             (tree-contains? (right tree) val)))))
```

What's that weird `contents` thing?
  : `(contents tree)` gives you the value at the root of the tree.

Could you rewrite that with conditionals?
  : Sure

```
(define tree-contains?
  (lambda (tree val)
    (cond
      ; If the tree is empty, it can't contain val
      [(empty? tree)
       #f]
      ; If the root of the tree is val, the tree contains val
      [(equal? val (contents tree))
       #t]
      ; If the left subtree contains val, the whole tree contains val
      [(tree-contains? (left tree) val)
       #t]
      ; If the right subtree contains val, the whole tree contains val
      [(tree-contains? (right tree) val)
       #t]
      ; That's all folks
      [else
       #f])))
```

I'm having trouble with problem 4.  Is there a pattern that will help?  And can you help me understand that pattern?
  : Sure.
  : We are doing something with each element of a vector, or at least potentially doing something with each element of a vector.
  : Note to self: My quick scan of the vectors reading suggests that to change one element, I do `vector-ref`, compute a new value, and then call `vector-set!`.

```
;;; Procedure:
;;;   number-vector-increment!
;;; Parameters:
;;;   vec, a vector
;;; Purpose:
;;;   Increment the value at all vector positions
;;; Produces:
;;;   [Nothing; called for side effect.]
;;; Preconditions:
;;;   (vector-ref vec index) for 0 <= index < (vector-length vec) is a number.
;;;   number-vect-increment-at! is defined
;;; Postconditions:
;;;   Let val be (vector-ref vec index) before the procedure call. After the
;;    call (vector-ref vec index) produces val+1.
(define number-vector-increment!
  (lambda (vec)
    (let ([len (vector-length vec)]) ; unchanging value, tells recursion to stop
      (let kernel! ([pos 0])  ; Start the recursion at the first position
        (when (< pos len) ; When the position is valid,
          (number-vector-increment-at! vec pos) ; increment the number at pos
          (kernel! (+ 1 pos))))))) ; and process the rest of the vector

```

How do I return nothing from a procedure?
  : Use the `when` approach above.

Can I return the vector in `vector-selective-map!`?
  : Sure.  No penalty for doing so.

For the whatzitdo, will you help with `(l-s apply darkwing)`?
  : Nope.
  : But think about `((l-s apply darkwing) '(duck1 duck2 duck3))` as being a long way
    to write `(darkwing duck1 duck2 duck3)`

Can you help on problem 5?
  : Apparently not.
  : Keep one vector for all the tallies.
  : Instead of incrementing when you recurse, just use the increment-vector-position (or whatever it's called)

What should #2 do on the null list
  : Crash and burn.
  : Produce a helpful error message like "it appears that you attempted to set something in the empty list; I am confused.  I think I'll start to sing a song.  Daisy Daisy tell me your answer ..."

Do we have to write error messages in our procedures?
  : No.

Can you give us hints on c and d?
  : c: The string at the root is larger (alphabetically/lexicographically follows) the largest (alphabetically/lexicaraphically last) string in the left subtree.
  : d: Similar
  : `string>=` or `string-ci>=` is "lexigraphically follows".

Do we have to write test suites?
  : No.

Can we write a long let* statement in the last one.
  : Sure.

Is there a racket style guide?
  : Even if there is, it probably does not meet my standards.
  : Use ctrl-I
  : Put spaces between parameters
  : Don't put close parens on a line by themselves.
  : Choose meaningful variable names.
  : DON'T CHOOSE MISLEADING VARIABLE NAMES
  : When lines are long, have one parameter per line.
  : `(lambda (params)` goes on a line by itself, at least at the top level.
  : Sam's judgement is fickle.

Can I rearrange the code and put the helpers at the top?
  : Sure.

What's with the Daisy thing?
  : 2001.

Lab
---

Why is there so much copying and pasting in this lab?
  : We hope that it's copy/paste/READ (or READ/copy/paste).
  : Because we are trying to model an approach in which you learn to
    annotate code, but want to make sure you understand how to annotate.

What do you mean by "total calls"?
  : `reverse` + `append` + `cons` + `car` + `cdr` + `null`

What is the lab writeup?
  : Exercise 6

Really?
  : Yes.

What's the problem with `alphabetically-first-4`?
  : If you do exercise 5, you'll see that there are a *lot* of calls
    to `list-of-strings?`.
  : You want fewer such calls.

How do I achieve that goal?
  : Once you've figured out.
Debrief
-------
