Skip to main content

CSC 151.01, Class 21: Numeric recursion

Overview

  • Preliminaries
    • Notes and news
    • Upcoming work
    • Extra credit
    • Questions
  • Reflections on Readings
  • Lab
  • Debrief (?)

News / Etc.

  • Quizzes 6 and 7 returned. We may want to chat a bit about quiz 6.
  • At the end of today’s class, you will be half way through CSC 151. Congratulations!
  • Quiz Friday: Identify your classmates.
    • Reminder: Send me your nickname if P’Web does not match.
  • Review sessions this week are on recursion. Yay! (Mentors will also have printed picture lists.)

Upcoming Work

Extra credit (Academic/Artistic)

  • Trio TONIGHT at 7:30 p.m. in Sebring-Lewis.

Extra credit (Peer)

Extra credit (Misc)

Other good things

Notes on quiz 6

;;; Procedure:
;;;   sort-by-latitude
;;; Parameters:
;;;   zips, a list
;;; Purpose:
;;;   Sort the entries of zips by latitude
;;; Produces:
;;;   sorted, a list
;;; Preconditions:
;;;   * zips is a list of lists.
;;;   * (or) Each element of zips is a traditional zip code entry
;;;     of the form (zip latitude longitude cityname statename countyname)
;;;   * Each element of zips is a list of length at least two.
;;;   * The second element (cadr) of each of those lists represents
;;;     latitude.
;;;   * The latitude (cadr) of each of those lists is real.
;;; Postconditions:
;;;   * sorted contains the same set of elements as the original list,
;;;     potentially in a different order. "sorted is a permutation of
;;;     zips"
;;;   * sorted is sorted by latitude in increasing order from least
;;;     to greatest.  For any two consecutive elements of list,
;;;     entry1 and entry2, (< (cadr entry1) (cadr entry2)).
(define sort-by-latitude
  (let ([smaller-latitude
          (lambda (zip1 zip2)
            (< (cadr zip1) (cadr zip2)))])
    (lambda (zips)
      (sort zips smaller-latitude))))

Questions

Reading review

Discuss with your partner.

What were the primary points of Monday’s class?

  • You will find that there are a few templates that work well for recursion (and for other tasks). Look for common solutions and then write down the commonality to use in the future.
  • How reduce is written: Using a binary procedure to combine elements of a list (to find extreme values, or other combinations)
  • How to write list predicates.
  • Practice with recursion is useful as we learn it.
  • Be careful about the number of recursive calls; you can accidentally create really inefficient recursive procedures.

What were the primary points of today’s reading?

  • We can do recursion with numbers as parameters.
  • Be careful about the base case and how you get closer to the base case. (E.g., if the base case is “the parameter = 1” and you start at -1, subtracting 1 does not get you closer.
  • For numeric recursion, n=0 and n=1 (a.ka. (= n 0) and (= n 1)) are the most common base cases.
  • Doing a reading the same night the exam is due is hard.
  • Sometimes rather than counting down to zero, we count up to n.
  • Sometimes instead of adding/subtracting 1, we divide/multiply.

Lab

Why am I getting a dot in the middle of my list?
If you cons onto something other than a list, DrRacket adds a period right before the end to say “this is not a list”.
Why doesn’t DrRacket just issue an error?
cons was designed to be used to build things other than lists.

Sam asks: Can you write my-iota without reverse?

Do any of the patterns fit for my-iota?
Not quite. But counting upwards is helpful.

Writeup: Exercise 5. Documentation is not necessary.

Debrief

Let’s write my-iota without reverse. The hint says to write a helper. The typical helper has two columns: What we’ve computed so far and what we have remaining.

so-far      remaining
------      ---------

How should those start?

so-far starts as null, remaining starts as 5.

What are we going to do at each step?

  • subtract 1 from remaining
  • add that value to the front of so-far
(define my-iota-helper
  (lambda (so-far remaining)
    (display (list 'my-iota-helper so-far remaining)) (newline)
    (if (zero? remaining)
        so-far
        (my-iota-helper (cons (- remaining 1) so-far)
                        (- remaining 1)))))

(define my-iota
  (lambda (n)
    (when (or (not (integer? n)) (negative? n))
      (error "my-iota: Expects non-negative integer, given" n))
    (my-iota-helper null n)))