# Class 18: Recursion with Lists (2)

Back to Recursion with Lists (1). On to Preconditions and Postconditions.

Held: Friday, 26 September 2003

Summary: Today we continue our exploration of recursion over lists.

Related Pages:

Notes:

• I've evaluated your Homework 2. Please let me know if you have questions on the evaluation.
• Are there questions on Exam 1?
• Have a great weekend.

Overview:

• Lab.
• Reflection: `member?` and more.

## Some Particulars

### Tallying Skips

• Here's one way to tally the skips in a list
• If the list is empty, there are no skips.
• If the first element of the list is `skip`, add 1 to the number of skips remaining in the list.
• Otherwise, count the number of skips remaining in the list.
• Here's that strategy in code.
```(define tally-skips
(lambda (lst)
(cond
((null? lst) 0)
((eq? (car lst) 'skip)
(+ 1 (tally-skips (cdr lst))))
(else (tally-skips (cdr lst))))))
```
• Let's watch it in action
```(tally-skips (list 'hop 'skip 'jump 'and 'skip 'again))
=> (tally-skips (list 'skip 'jump 'and 'skip 'again))
=> (+ 1 (tally-skips (list 'jump 'and 'skip 'again)))
=> (+ 1 (tally-skips (list 'and 'skip 'again)))
=> (+ 1 (tally-skips (list 'skip 'again)))
=> (+ 1 (+ 1 (tally-skips (list 'again))))
=> (+ 1 (+ 1 (tally-skips (list))))
=> (+ 1 (+ 1 0))
=> (+ 1 1)
=> 2
```
• One might also decide to add an extra parameter, `tally`, that keeps track of each skip seen.
• If there are no values left in the list, use `tally`
• If the first value is `skip`, add 1 to `tally` and continue with the remaining values.
• Otherwise, just use the same `tally` and continue with the remaining values.
• In code,
```(define tally-skips-helper
(lambda (lst tally)
(cond
((null? lst) tally)
((eq? (car lst) 'skip)
(tally-skips-helper (cdr lst) (+ 1 tally)))
(else (tally-skips-helper (cdr lst) tally)))))
```
• Let's watch it in action
```(tally-skips-helper (list 'hop 'skip 'jump 'and 'skip 'again) 0)
=> (tally-skips-helper (list 'skip 'jump 'and 'skip 'again) 0)
=> (tally-skips-helper (list 'jump 'and 'skip 'again) 1)
=> (tally-skips-helper (list 'and 'skip 'again) 1)
=> (tally-skips-helper (list 'skip 'again) 1)
=> (tally-skips-helper (list 'again) 2)
=> (tally-skips-helper (list) 2)
=> 2
```
• We need `tally` to start as 0, so we write the primary procedure as
```(define tally-skips
(lambda (lst)
(tally-skips-helper lst 0)))
```
• What should you do if you want to watch one of these in action on a different input? You can insert some calls to `display` in the body of the procedure. For example,
• In Scheme ...
```(define tally-skips-helper
(lambda (lst tally)
(display (list 'tally-skips-helper lst tally))
(newline)
(cond
((null? lst) tally)
((eq? (car lst) 'skip)
(tally-skips-helper (cdr lst) (+ 1 tally)))
(else (tally-skips-helper (cdr lst) tally)))))
```
• This strategy won't work nearly as well with the first `tally-skips`.

### Membership

• You may even find that there are some boolean procedures that don't even need an `if`. For example, here's one implementation of member that works on the principle that `val` is a member of `lst` if the list is not empty and `val` is the first element of `lst` or `val` is a member of the remainder of `lst`.
```(define member?
(lambda (val lst)
(and (not (null? lst))
(or (equal? val (car lst))
(member? val (cdr lst))))))
```

## History

Thursday, 28 August 2003 [Samuel A. Rebelsky]

• Created automatically.

Back to Recursion with Lists (1). On to Preconditions and Postconditions.

Disclaimer: I usually create these pages on the fly, which means that I rarely proofread them and they may contain bad grammar and incorrect details. It also means that I tend to update them regularly (see the history for more details). Feel free to contact me with any suggestions for changes.

This document was generated by Siteweaver on Tue Dec 9 13:59:40 2003.
The source to the document was last modified on Mon Sep 1 13:30:51 2003.
This document may be found at `http://www.cs.grinnell.edu/~rebelsky/Courses/CS151/2003F/Outlines/outline.18.html`.

You may wish to validate this document's HTML ; ; Check with Bobby

Samuel A. Rebelsky, rebelsky@grinnell.edu