---
title: Eboard 21  Numeric recursion
number: 21
section: eboards
held: 2017-10-11
---
CSC 151.03, Class 21:  Numeric recursion
========================================

_Overview_

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

### News / Etc.

* Quizzes 6 and 7 returned.  We may want to chat a bit about quiz 6.
* At the end of today's class, you will be half way through CSC 151.
  Congratulations! 
* Quiz Friday: Identify your classmates.
      * Reminder: Send me your nickname if P'Web does not match.
* Review sessions this week are on recursion.  Yay!  (Mentors will also
  have printed picture lists.)

### Upcoming Work

* [Writeup for class 20](../writeups/writeup20) due TONIGHT at 10:30 p.m.
    * Exercise 4 (do nocumentation necessary)
    * To: <csc151-03-grader@grinnell.edu>
    * Subject: CSC 151.03 Writeup 20 (YOUR NAMES)
* [Writeup for class 21](../writeups/writeup21) due Friday at 10:30 p.m.
    * Exercises 4d and 4e
    * To: <csc151-03-grader@grinnell.edu>
    * Subject: CSC 151.03 Writeup 21 (YOUR NAMES)
* Exam 2 cover sheets due NOW!
* Exam 2 epilogue due TONIGHT at 10:30 p.m.

### Extra credit (Academic/Artistic)

* Aspen Trio on Wednesday at 7:30 p.m.

### Extra credit (Peer)

### Extra credit (Misc)

### Other good things

### Quiz 6

```
;;; Procedure:
;;;   sort-by-latitude
;;; Parameters:
;;;   zips, a list
;;; Purpose:
;;;   Sort the entries of zips by latitude
;;; Produces:
;;;   sorted, a list
;;; Preconditions:
;;;   * Form of the data: Each entry of zips is a standard "zip code record"
;;;     Which is a list of the form '(zip latitude longitude cityname 
;;;     statename county).
;;;   * Alternate form of the data: Each entry of zips is a list.  The
;;;     the second element (cadr) of each entry represents the latitude
;;;     associated with with that entry.
;;;   * Limitations on the data: The latitiude of each entry is real.
;;;   * Alternate limitation: The cadr of each list in zips is real.
;;;   * Combined: Each entry of zips is a standard "zip code record"
;;;     Which is a list of the form '(zip:string latitude:real longitude:real 
;;;     cityname:string statename:string county:string).
;;; Postconditions:
;;;    * Relationship of result to input.  The same values are in sorted as
;;;      were in zips, but not necc in same order.  "sorted is a permutation 
;;;      of zips"
;;;    * Additional characteristics of the result.  The elements of sorted
;;;      are arranged by latitude (cadr). 
;;;    * More formal: For any two consecutive elements of sorted, a and b,
;;;      (<= (cadr a) (cadr b)).
(define sort-by-latitude
  (let ([smaller-latitude
          (lambda (zip1 zip2)
            (< (cadr zip1) (cadr zip2)))])
    (lambda (zips)
      (sort zips smaller-latitude))))
```

### Questions

Why do I get a weird dot in my list?
  : Because you are using `cons` with a value other than a list.

What work for CSC 151 do we have during break?
  : There will be a reading for the Monday after break.  That's it.
    No homework assignments.  No exams.

Notes from the readings
-----------------------

_Discuss with your partner ..._

What were the key takeaways from Monday's class?

* We need practice with recursion over lists.  It is not trivial and
  like many non-trivial things takes a lot of practice.
    * Reinforcement: "trust the recursion fairy"
    * "But don't make her/him/it/they/zi work too hard."
* Learned that `let` expressions can make our code both clearer and
  more efficient.
* If you don't write your recursive procedures well, you may find that
  your procedure runs *much* slower than you expect.  The typical problem
  is two identical recursive calls.
* You will find that you eventually start writing very similar code again
  and again to solve similar problems.  You should find the patterns in
  those similar pieces of code.  Then, your future solutions are
  "can I use this pattern"?
    * Pattern: "Most extreme value in list"
    * Pattern: Reduce with binary operator
* Note: Applying patterns requires some adaption.
* How to write list predicates, such as "all-real?" or "any-real?"

What did we do on Monday?

* Used conditionals in our recursive procedures.
* Used multiple kinds of recursion, both direct and helper.

What were the key takeaways from the reading on numeric recursion?

* You can write a recursive procedure that does not use lists, but instead
  uses numbers.
    * It's recursive in that it calls itself.
    * At each step, we need to get closer to the base case.
    * Here, the typical base case is a small number and we subtract
      or divide to get closer to that small number.
    * There are other methods of "simplification".  Our goal is just
      "get closer to base case."
* It still looks basically the same as list recursion.
* Sometimes, we want to count up to n rather than down to 0.  That will
  require a helper procedure.

Lab
---

Debrief
-------

Three ways to write `my-iota`.

```
(define my-iota-0
  (lambda (n)
    (reverse (my-iota-helper-0 n))))

(define my-iota-helper-0
  (lambda (n)
    (if (zero? n)
        null
        (cons (sub1 n) (my-iota-helper-0 (sub1 n))))))
```

```
(define my-iota-1
  (lambda (n)
    (my-iota-helper-1 null n)))

(define my-iota-helper-1
  (lambda (so-far remaining)
    (if (zero? remaining)
        so-far
        (my-iota-helper-1 (cons (sub1 remaining) so-far)
                          (sub1 remaining)))))
```

```
(define my-iota-2
  (lambda (n)
    (my-iota-helper-2 0 n)))

(define my-iota-helper-2
  (lambda (i n)
    (if (zero? n)
        null
        (cons i (my-iota-helper (add1 i) (sub1 n))))))
```
