---
title: Eboard 25  Numeric recursion
number: 25
section: eboards
held: 2019-04-05
link: true
---
CSC 151 2019S, Class 25:  Numeric recursion
===========================================

_Overview_

* Preliminaries
    * Notes and news
    * Upcoming work
    * Extra credit
    * Friday PSA
    * Questions
* Quiz
* Lab!

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

### News / Etc.

* Sit with your homework partner.
* Donuts for early arrivals.
* Mentor sessions Wednesday 8-9 p.m., Thursday 8-9 p.m., Sunday 5-6 p.m.
* Prospies on Monday!
* Reminder to Sam: talk about efficiency on Monday.

### Upcoming work

* [Homework 7](../assignments/assignment07) due Tuesday.  
* Next Friday's quiz: Recursion
* Reading for Monday: 
  [Local Bindings](../readings/letrec)
* Lab writeup: Exercise 4
    * CSC 151.01 2019S Writeup for class 25 (Your Names)

### Extra Credit

_I would certainly appreciate suggestions of other extra credit activities
(preferably via email)._

#### Extra credit (Academic/Artistic)

* Isabelle Demers concert, Saturday at 3:00 p.m. in Herrick

#### Extra credit (Peer)

* Grinnell Singers, Sunday the 7th at 2pm
* Women's golf, Saturday/Sunday somewhere in St. Louis
* Track and Field, Saturday at Cornell College (up the road, not in NY)

#### Extra credit (Wellness)

#### Extra credit (Wellness, Regular)

* 30 Minutes of Mindfulness at SHACS every Monday 4:15-4:45
* Any organized exercise.  (See previous eboards for a list.)
* 60 minutes of some solitary self-care activities that are unrelated to
  academics or work.  Your email reflection must explain how the activity
  contributed to your wellness.
* 60 minutes of some shared self-care activity with friends. Your email
  reflection must explain how the activity contributed to your wellness.

#### Extra credit (Misc)

* Participate in Kinetic Sculpture Competition: Saturday the 27th
    * You'll need to build your sculpture in advance
* Wednesday the 10 at 4pm on Mac Field: Giant Laurel Leaf.  (Free t-shirt!)
* Scarlet and Give Back Day next Wednesday/Thursday (I think).  If you
  don't have money to donate, let me know and I will give you $5 on
  Monday to donate.  (That does commit you to donating and to writing
  something.)

### Other good things 

### Other things

* One of the advantages of going to Grinnell is that presidential candidates
  come to Iowa regularly.  Take advantage of those visits.  Beto O'Rourke
  is at Hotel Grinnell today at 5:30 p.m.  Go and ask him about his
  hacker group.

### Friday PSA

* You are awesome; please take care of yourselves.

### Questions

How do you add to the end of a list?

> Adding to the end of a list is inefficent, try to avoid it.

> `(append lst (list val))`

If I'm building a list, how do I avoid adding to the end?

> Add to the front.

> If you are doing things recursively, the list may end up backwards.
  Reverse it.

For `indices-of`, how do I keep track of the position?

> Have a helper that has multiple parameters, in addition to remaining
  have a `pos` that represents the position in the original list.

Could we review tail recursion?

> Sure.

> Traditional (direct) recursion, we recurse and then do something with the
  result.

    ; count the number of odd values in a list
    (define tally-odds
      (lambda (lst)
        (cond
          [(null? lst)
           0]
          [(odd? (car lst))
           (+ 1 (tally-odds (cdr lst)))]
          [else
           (tally-odds (cdr lst))])))

    (tally-odds '(4 1 2 3 2))
    => (tally-odds '(1 2 3 2))
    => (+ 1 (tally-odds '(2 3 2)))
    => (+ 1 (tally-odds '(3 2)))
    => (+ 1 (+ 1 (tally-odds '(2))))
    => (+ 1 (+ 1 (tally-odds '())))
    => (+ 1 (+ 1 0))
    => (+ 1 1)
    => 2

> Note that direct recursion builds up a lot of delayed work.  Some
  people like to avoid building up that delayed work.  We do that
  with helper recursion.
    
    ; Select all the odds in a list of values
    (define select-odds
      (lambda (lst)
        (select-odds-helper lst null)))
    (define select-odds-helper
      (lambda (remaining odds-so-far)
        (cond
          [(null? remaining)
           (reverse odds-so-far)]
          [(odd? (car remaining))
           (select-odds-helper (cdr remaining)
                               (cons (car remaining) odds-so-far))]
          [else
           (select-odds-helper (cdr remaining) odds-so-far)])))

Why do you make helpers to do tail recursion?

> In most cases, you need a parameter to keep track of the "result so far".

How do you pronounce words beginning with C

> car (cah)

> cdr (could-err)

> cadr (cad err)

> cddr (cuh did err)

> Gloucester Marina 

Could we write `select-odds` directly?

> Sure.

    (define select-odds
      (lambda (lst)
        (cond
          [(null? lst)
           null]
          [(odd? (car lst))
           (cons (car lst) (select-odds (cdr lst)))]
          [else
           (select-odds (cdr lst))])))

> But we'll build up a lot of cons's.

Generalizing

    (define select
      (lambda (pred? lst)
        (cond
          [(null? lst)
           null]
          [(pred? (car lst))
           (cons (car lst) (select pred? (cdr lst)))]
          [else
           (select pred? (cdr lst))])))

Lab
---

Plan time to work on homework with your partner.

Note "iota" is our old name for "range" (or at least the one-parameter "range")

To add something to the front of a list, please use `(cons val lst)` 
rather than `(append (list val) lst)`.

Lab writeup: Problem 4

* CSC 151.01 2019S Writeup for class 25 (Your Names)
* For 4c: Recursion requires that when you make a recursive call, the
  parameter is closer to the base case.  How do you know that the 
  parameter to the recursive call is closer to the base case?  How
  have you made it simpler?  (E.g., have you removed the leftmost or
  rightmost digit?)

Defining iota

   (define iota
     (lambda (n)
       (iota-helper 0 n)))
   (define iota-helper
     (lambda (start n)
       (if (= start n)
           null
           (cons start (iota-helper (+ start 1) n)))))

or

    (define iota
      (lambda (n)
        (iota-helper (- n 1) null)))
    (define iota-helper
      (lambda (n so-far)
        (if (< n 0)
            so-far
            (iota-helper (- n 1) (cons n so-far)))))
