Skip to main content

CSC 151.03, Class 17: Recursion basics

Overview

  • Preliminaries
    • Notes and news
    • Upcoming work
    • Extra credit
    • Questions
  • Background
  • Key ideas in recursion
  • Sample recursive procedures
  • Expressing recursion in Scheme

News / Etc.

  • New partners.
  • Please make sure to return your computer cards to the jar.
  • Exam 1 returned last night. I’m happy to take questions via email or during office hours. (I’ll probably add some office hours this week.)
  • I have not had time to grade your quizzes. You may get them back on Wednesday (but you may not).
  • Dates in formats like “10/02/2017” are ambiguous. Dates like “10/02/17” are even worse. Please use “YYYY-MM-DD” format for your dates.
  • Please discuss which switching format you and your partner will use
    • Switch every ten minutes
    • Switch midway through class
    • Switch every problem

Upcoming Work

Extra credit (Academic/Artistic)

  • Any of the many Grinnell Prize events.
  • CS Table, Tuesday: Tapia. Noon, in the first Private Dining Room at the top of the stairs in the cafeteria.
  • Wednesday, 5pm, Mary Beth Tinker Talk
  • Nice Fish this weekend

Extra credit (Peer)

  • The Neverland players will be performing ThFriSu at 7pm and Sa at 2pm in the Wall theatre in Bucksbaum.
  • Stand up comedy club Friday night 8:00 with Free Pizza
  • Latin American Festival Saturday 5pm-8pm in Sebring-Lewis
  • Ritalin Test Squad in Loose Lounge 9:30

Other good things

  • Mid-fall festival - Friday at 6:20 in Harris Gym (free food!)
  • Volleyball vs. Knox Tuesday at 7:00 p.m.
  • Men’s Soccer Wednesday at 4:30 vs. Cornell
  • Volleyball vs. Beloit, Friday at 7:00 p.m.
  • Women’s Tennis vs. St. Norbert, Saturday at 9:00 a.m.
  • Volleyball vs. Lake Forest, Saturday at 1:00 p.m.
  • Women’s Tennis vs. Ripon, Saturday at 3:00 p.m.
  • Football, Saturday at 1:00 p.m.

Questions

Can you help on problem 3?
Sure.
Your goal is to find nearby words.
As we suggested in the assignment, the first step should be to make lists of nearby words.
Suppose our original text was '("twas" "brillig" "and" "the" "slithy" "toves" "did" "gyre")
We will make three “successor lists” of equal length.
  • '("brillig" "and" "the" "slithy" "toves" "did" "gyre" "")
  • '("and" "the" "slithy" "toves" "did" "gyre" "" "")
  • '("the" "slithy" "toves" "did" "gyre" "" "" "")
We can use map to combine them together
We can use filter to extract all of the lines that begin with a certain word
We can use some clever combination of operations to put them back together.
But you made those four lists manually. We’re reading the words from a
file. What should we do?
Can you write a program that drops the first word and adds “” at the end? (Yes)
Use it four times. It will probably be faster than me doing it by hand anyway.

Will you put the code you just wrote in the eboard?

(define words0 (list "the" "students" "flash" "card" "was" "not" "in" "the" "bin" "because" "students" "are" "awesome"))
(define words1 (list "students" "flash" "card" "was" "not" "in" "the" "bin" "because" "students" "are" "awesome" ""))
(define words2 (list "flash" "card" "was" "not" "in" "the" "bin" "because" "students" "are" "awesome" "" ""))
(define words3 (list "card" "was" "not" "in" "the" "bin" "because" "students" "are" "awesome" "" "" ""))
> (list (car words0) (car words1) (car words2) (car words3))
'("the" "students" "flash" "card")
> (map list words0 words1 words2 words3)
'(("the" "students" "flash" "card")
  ("students" "flash" "card" "was")
  ("flash" "card" "was" "not")
  ("card" "was" "not" "in")
  ("was" "not" "in" "the")
  ("not" "in" "the" "bin")
  ("in" "the" "bin" "because")
  ("the" "bin" "because" "students")
  ("bin" "because" "students" "are")
  ("because" "students" "are" "awesome")
  ("students" "are" "awesome" "")
  ("are" "awesome" "" "")
  ("awesome" "" "" ""))
> (filter (lambda (foursome)
            (equal? (car foursome) "students"))
           (map list words0 words1 words2 words3))
'(("students" "flash" "card" "was")
  ("students" "are" "awesome" ""))
Can we use recursion on HW5?
No.
What’s that mantra again?
I’m still working on it.
“Solve for one, then for all.”
AO will write a better one.
I’m struggling. I feel like it’s only me.
It’s not. We’re doing hard stuff. But historical evidence suggests that you will make it.
Do we really get the next exam on Wednesday?
Yes.
How do I figure out how many pages are in a book?
That’s a good question. I’d look for it on Google books or Amazon and see what they say.
100K to 200K words is appropriate. (K is short for Kilo and can mean 1000 or 1024.)
Will you provide us with suggested flash cards?
Maybe.
Can/should we play with the axes on problem 2?
You can, you need not.
Should we label the axes
You should, but you need not.

Background

What are the basic building blocks of algorithms? (Name/Meaning/Example in Scheme)

  • Sequencing: Specifying or understanding the order in which operations are done. Parenthesis group a function and its parameters. Nested paranethesis go inside out. o has its parameters evaluate/apply right to left, and evaluates parameters left to right, map and reduce have unknown order.
  • Input and output: Stuff you give to the algorithm and stuff it gives you back.
  • Conditional: Stuff only happens if a certain condition is met.a
    • (if condition consequent alternate)
    • cond
    • when
    • Certain uses of and and or
  • Variables: Assign a name to a value. (define fred 5). Also let and let*.
  • Subroutines: Helper procedures. (define proc (lambda ...)) (define proc (o ...)), (define proc (section ...)).
  • Repetition: Do the same or similar things again and again. We can use map and map1 and reduce
  • Basic values and operations: Things that are pre-defined or defined in a library. For example, + and - and length

You have everything you need to build algorithms. But we can learn more about each.

Today we are covering a general form of repetition. It’s called recursion.

Sample recursive procedures

To find out how many cards in a stack

  • If there’s nothing in the stack
    • Say zero
  • Otherwise
    • Remove one
    • Ask your assistant to count the rest
    • Add 1

To find out which words in a list contain a

  • If there are no students
    • Return the empty list
  • Otherwise
    • Look at the first student
    • If it does not contain the letter “a”
      • Ask your assistant to do the same thing on the remaining names
      • Return the same value
    • Iif it does contain the letter “a”
      • Ask your assistant to do the same thing on the remaining names
      • Add yours

Helper

(define contains-a?
  (lambda (word)
    (<= 0 (index-of #\a (string->list word)))))
(define contains-a?
  (lambda (word)
    (let ([letters (string->list word)])
      (or (<= 0 (index-of #\a letters))
          (<= 0 (index-of #\A letters))))))

Key ideas in recursion

  • Assumption: You can solve a problem by solving a simpler version of the same problem and then “fixing up”
    • Assume that the procedure you are writing works, then use it within your procedure.
    • Having a procedure call itself is called “recursion”
  • Note: Recursion has to eventually stop. We call the condition for whether or not it stop the “base cas check”a and “base case value”

Expressing recursion in Scheme

Write filter-a, which extracts the words in a list that contain a

(define filter-a
  (lambda (lst)
    ; If there is nothing in the list
    (if (null? lst)
        ; Return the empty list
        (list)
        ; Otherwise
        (let ([first (car lst)])
          ; Look at the first student
          ; If it does contain the letter "a"
          (if (string-contains? first "a")
              ; Ask your assistant to do the same thing on the remaining names
              ; Add yours
              (cons first (filter-a (cdr lst)))
              ; If it does not contain the letter "a"
              ; Ask your assistant to do the same thing on the remaining names
              ; Return the same value
              (filter-a (cdr lst)))))))

Helper

(define contains-a?
  (lambda (word)
    (<= 0 (index-of #\a (string->list word)))))
(define contains-a?
  (lambda (word)
    (let ([letters (string->list word)])
      (or (<= 0 (index-of #\a letters))
          (<= 0 (index-of #\A letters))))))