---
title: Eboard 22  Recursion with helper procedures
number: 22
section: eboards
held: 2019-03-15
link: true
---
CSC 151 2019S, Class 22:  Recursion with helper procedures
==========================================================

_Overview_

* Preliminaries
    * Notes and news
    * Upcoming work
    * Extra credit
    * Spring break PSA
    * Questions
* Quiz
* Lab

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

### News / Etc.

* Mentor sessions Wednesday 8-9 p.m., Thursday 8-9 p.m., Sunday 5-6 p.m.
    * No mentor session the Sunday at the end of break.
* I was sorry to see so few of you at Convocation yesterday.  It was
  one of the best ones that I've been to in recent years.
* I expect to have grading completed by the end of spring break.

### Upcoming work

* No reading for Monday after break.
* Lab writeup: TBD.

### Extra Credit

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

#### Extra credit (Academic/Artistic)

#### Extra credit (Peer)

* Grinnell Singers, St. Paul Saturday, Rochester Sunday, ...
* Women's Golf, Amelia Island, last Sunday of spring break.
* Track and Field, Emory University, last weekend of spring break.

#### Extra credit (Wellness)

* Sub-free spring break

#### 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)

### Other good things 

### Spring break PSA

* Relax.  Enjoy time away from Grinnell.
* Moderation in everything (or almost everthing; catching up on sleep
  is fine)
* Consent is essential.

### Questions

_A few of us traded names this morning.  Will that effect the quiz?_

> For the quiz, you must use the name you have been using for the
  earlier part of the semester.

Quiz
----

Quick summary of the reading
----------------------------

We've been talking about recursion and have a basic model of 
recursion.

* You have your base case (and a base-case test)
* You also have a recursive case which consists of 
    * A simplification procedure (e.g., cdr)
    * A recursive call
    * A combination/postprocessing

```drracket
(define proc
  (lambda (lst)
    (if (null? lst)
        base-case
        (combine (car lst) (proc (cdr lst))))))
```

```drracket
(define product
  (lambda (lst)
    (if (null? lst)
        1
        (* (car lst) (product (cdr lst))))))
```

```drracket
(define product 
  (lambda (lst)
    (if (singleton? lst)
        (car lst)
        (* (car lst) (product (cdr lst))))))
```

Detour: What's going on with nested tick marks (nested quotes)?

* `'thing` is a shorthand for `(quote thing)`
* `(quote thing)` is a keyword that says "take this thing as is, without
  evaluating it"
* `(quote 5)` is 5
* `(quote sam)` is `'sam`
* `(quote '(1 2 3))` is a three-element list

```drracket
> (quote 5)
5
> (quote sam)
'sam
> (quote (1 2 3))
'(1 2 3)
> (quote (1 (+ 2 3) 4))
'(1 (+ 2 3) 4)
> (list? (list-ref (quote (1 (+ 2 3) 4)) 1))
#t
> (list? (list-ref (quote (1 (quote a) 2)) 1))
#t
>  (list-ref (quote (1 (quote a) 2)) 1)
''a
> '(quote a)
''a
> (list-ref '(1 'a 2) 1)
''a
> (quote (and a))
'(and a)
> (quote (quote a))
''a
> (list? (quote (quote a)))
#t
```

The moral?  Don't nest tick marks.  It makes everbody's brain hurt.

`</detour>`

Back to recursion.

Observation: Sometimes we want to keep track of extra values while
recursing.  E.g., reversing a list.

```text
remaining       so-far
---------       ------
'(a b c)        '()
  cdr             cons
'(b c)          '(a)
  cdr             cons
'(c)            '(b a)
  cdr             cons
'()             '(c b a)
```

Let's try expressing that in Scheme.  Is this correct?

```drracket
(define rev
  (lambda (lst)
    (if (null? lst)
        null
        (cons (car lst) (rev (cdr lst))))))
```

This does not work.  In fact, our model of thinking through the problem
suggests we need two parameters, rather than one.  But reverse should
only have one parameter.

```drracket
(define rev
  (lambda (lst)
    (rev-kernel lst '())))

(define rev-kernel
  (lambda (remaining reversed-so-far)
    (if (null? remaining)
        reversed-so-far
        (rev-kernel (cdr remaining)
                    (cons (car remaining) reversed-so-far)))))
```

It can be helpful to put in a call to `display`.

```drracket
(define rev
  (lambda (lst)
    (rev-kernel lst '())))

(define rev-kernel
  (lambda (remaining reversed-so-far)
    (display (list 'kernel remaining reversed-so-far)) (newline)
    (if (null? remaining)
        reversed-so-far
        (rev-kernel (cdr remaining)
                    (cons (car remaining) reversed-so-far)))))
```

Lab
---

No writeup!  It's break.

