CSC151.01 2014F, Class 34: Iteration
====================================

_Overview_

* Preliminaries.
    * Admin.
    * Upcoming Work.
    * Extra Credit.
    * Notes on the Quiz
    * Questions.
* Contrasting `map`, `for-each`, `repeat`, and recursion.
* Lab.

Preliminaries
-------------

### Admin

* Continue partners.
* I hope you had a wonderful weekend and found ways to enjoy the sun.
* Extended/modified office hours continue this week.  (More variation than
  normal; sorry.)
* Welcome to any prospective students!
* It's preregistration time.  Let me know if you have questions about
  our other courses.  (Email or in-person visits will be fine.)
* Some students in a review session said (approximately): "No one reads
  the eboards" (presumably, except during class).  Double-blind vote (Evan
  counts, you and I close our eyes): 
    * Do you find eboards useful?
    * Should I keep doing eboards?
* Quiz 8 will be returned during lab.  We will go over it first.

### Upcoming Work

* No lab writeup!
* Reading for Tuesday:
  [Pairs and Pair Structures](../readings/pairs-reading.html)
* [Homework 6](../assignments/assignment.06.html), due Tuesday night.
* Exam 3, distributed this coming Wednesday, due the following Tuesday.

### Cool Upcoming Events on Campus 

* Peace studies talk Monday the 10th at 4:15 p.m. in JRC 101.
  "With All the World’s Violence, Where is Peace?"

### Extra Credit Opportunities

#### Academic

* CS Extra Thursday: Study-Abroad Opportunities with a CS Component
  4:30 p.m., Science 3821.
* CS Table Friday: Open Data.
  12:10-12:50, Day PDR.

#### Peer Support

* Karan's radio show 11pm Thursday nights on KDIC
* Evan's radio show 5pm Friday nights on KDIC
* Donna's radio show Sunday midnight on KDIC
* Swimming on Saturday (sometime) against that small, not-as-good, liberal
  arts college in Northeast Iowa.
* The gaming club is hosting a Smash Brothers tournament on Saturday
  (Loose at 11am ...)

### Notes on the Quiz

There was enough difficulty on the quiz that it seems worth going over.

#### Problem 1

    (define iota
      (lambda (num)
        (if (not (and (number? num)
                      (not (negative? num))
                      (integer? num)))
            (error "iota expected a non-negative integer, given " num)]
            (let kernel ([counter 0])
              (if _______________
                  null
                  (cons _______________  (kernel ___________________)))))))

* What's the goal of `iota`?  Make a list of the numbers from 0 to num-1.
* What are some sample inputs/outputs?
        > (iota 5)
        '(0 1 2 3 4)
        > (iota 1)
        '(0)
        > (iota 0)
        '()
* What does the test/consequent at the top do?  Makes sure that the input is 
  a non-negative integer.  If not, spews out an error message.
* What's that weird 'let kernel ([counter 0])'?  When we do the recursion,
  we have a variable, counter, which changes.  Its initial value is 0.
  `kernel` is our recursive procedure.
* What does the goal of `kernel` seem to be?  The part of the procedure
  that does the real work, now that we know that the parameter is "safe".
  Use the counter to make the list of the integers up to num-1.
* When counter is 0 and num is 3, should we do base case or recursive case?
  Recursive.
 
            (let kernel ([counter 0])
              (if _______________
                  null
                  (cons _______________  (kernel ___________________)))))))

* What should the first parameter to cons in the recursive case be?
    * Option 1: car of the list.  (What list?)
    * Option 2: Just the counter.  The winner!
* When we recurse, what should we do to counter?  Add 1 (see above.)

            (let kernel ([counter 0])
              (if _______________
                  null
                  (cons counter (kernel (+ 1 counter))))))))

* Can you generalize that last check?

            (let kernel ([counter 0])
              (if (= counter num)
                  null
                  (cons counter (kernel (+ 1 counter))))))))

#### Problem 2

    (define whatzitdo
      (lambda (vals)
        (and (not (null? vals))
             (or (odd? (car vals))
                 (whatzitdo (cdr vals))))))

* We asked for the *Purpose*, not the *Process*.
* Quick analysis.
    * What is (whatzitdo null)?  It's #f.
    * What is (whatzitdo (list "a")).  Error.
    * What is (whatzitdo (list 0))?  #f
    * What is (whatzitdo (list 1))?  #t
    * What is (whatzitdo (list 0 0))?  #f
    * What is (whatzitdo (list 0 1))?  #t
    * What is (whatzitdo (list 1 0))?  #t
    * What is (whatzitdo (list 1 1))?  #t

#### Followup

* Problem 1 does not count unless it helps you.

### Questions

Contrasting `map`, `for-each`, `repeat`, and recursion
------------------------------------------------------

* Next class.

Lab
---
