Skip to main content

CSC 151 2019S, Class 24: Patterns of list recursion

Overview

  • Preliminaries
    • Notes and news
    • Upcoming work
    • Extra credit
    • Questions
  • Looking for patterns
  • Patterns of list recursion (extreme, reduce, etc.)
  • List predicates

Preliminaries

News / Etc.

  • Oreos! (Up to 3)
  • Mentor sessions Wednesday 8-9 p.m., Thursday 8-9 p.m., Sunday 5-6 p.m.

Upcoming work

  • Homework 7 due Tuesday. Partners to be assigned this afternoon (sorry)
  • Friday’s quiz: Recursion
  • Reading for Friday: See the schedule
  • No flash cards this week.
  • Lab writeup: Problem 5

Extra Credit

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

Extra credit (Academic/Artistic)

  • Convo Thursday: Microbes: The links between soil, gut, and health
  • Noura Mint Seymali Concert tonight at 7:30 p.m. in Herrick
  • Isabelle Demers concert, Saturday at 3:00 p.m. in Herrick

Extra credit (Peer)

  • Grinnell Singers, Sunday the 7th at ???
  • Women’s golf, Saturday/Sunday somewhere in St. Louis
  • Track and Field, Saturday at Cornell College (up the road, not in NY)
  • Open mic night at Bob’s.

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)

  • 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 to donate.

Other good things

Questions

Lab

Ignore the zip code problems. Sorry about that!

Lab writeup: Problem 5

Debrief

Note: and and or stop as soon as they know their result.

  • (and ... #f .....) stops at the #f.
  • (or ... #t ...) stops at the #t.

Generalizing reduce

(define REDUCE-PROC
  (lambda (lst)
    (if (null? (cdr lst))
        (car lst)
        (PROC (car lst) (REDUCE-PROC (cdr lst))))))

(define REDUCE
  (lambda (proc lst)
    (if (null? (cdr lst))
        (car lst)
        (PROC (car lst) (REDUCE proc (cdr lst))))))

Extending contains-needle? for nested lists

; Using our pattern
(define contains-needle?
  (lambda (lst)
    (and (not (null? lst))
         (or (eq? 'needle (car lst))
             (contains-needle? (cdr lst))))))

; Rewritten less elegantly, but perhaps more helpfully
(define contains-needle?
  (lambda (lst)
    (cond
      [(null? lst) 
       #f]
      [(eq? (car lst) 'needle) 
       #t]
      [else
       (contains-needle (cdr lst))])))

; Add: Check in nested lists
(define contains-needle?
  (lambda (lst)
    (cond
      [(null? lst) 
       #f]
      [(eq? (car lst) 'needle) 
       #t]
      [(list? (car lst))
       (or
        (contains-needle (car lst))
        (contains-needle (cdr lst)))]
      [else
       (contains-needle (cdr lst))])))

What do you think about this solution for smallest?

(define smallest
  (lambda (lst)
    (cond
      [(null? (cdr lst))
       (car lst)]
      [(< (car lst) (smallest (cdr lst)))
       (car lst)]
      [else
       (smallest (cdr lst))])))

We discover that (smallest (range 100)) is really fast and (smallest (range 30)) is unbearably slow.

> (time (smallest (reverse (range 25))))
cpu time: 968 real time: 966 gc time: 8
0
> (time (smallest (reverse (range 26))))
cpu time: 1908 real time: 1905 gc time: 8
0
> (time (smallest (reverse (range 27))))
cpu time: 3780 real time: 3781 gc time: 8
0
> (time (smallest (reverse (range 28))))
cpu time: 8240 real time: 8240 gc time: 24
0