Skip to main content

CSC 151.03, Class 25: Pairs and pair structures

Overview

  • Preliminaries
    • Notes and news
    • Upcoming work
    • Extra credit
    • Friday PSA
    • Questions
  • Representing lists with pairs and cons cells
  • Lab
  • Debrief (?)

News / Etc.

Upcoming Work

  • Writeup for class 24 due TONIGHT at 10:30 p.m.
  • Writeup for class 25 due Monday at 10:30 p.m.
    • Exercises 1 and 3.
    • Hand to: Sam (or under Sam’s door)
  • Assignment 6 distributed.
    • Although the assignment says that you have assigned pairs, I will permit you to choose your own partners (or to work in individuals or trios).
  • Read Vectors for Monday’s class.
    • Still under development; should be ready tonight.
    • Sam will try to email you.

Extra credit (Academic/Artistic)

  • Protest Bot workshop, Friday 4pm in Burling 1st.
  • Rebirth Brass Band, Wednesday at 7:30 p.m. in Herrick Chapel.

Extra credit (Peer)

Extra credit (Misc)

Other good things

  • Art SEPC Pop-Up Show.
  • Grinnell Singers Sunday at 2 p.m. in Sebring Lewis.
  • Star Wars Music Talk TODAY at 4pm.
  • Swimming and Diving meet Saturday.
  • Tailgate party Saturday at 11:30 a.m.
  • Football Saturday

Friday PSA

Friday Survey

How is recursion going?

  • [4] It’s really hard. I really don’t feel like I’m getting anything.
  • [20] It’s hard, but I’m starting to get it. I can find “templates” and modify them to do what I want, but I don’t think I could write a recursive procedure from scratch.
  • [10] It’s hard, and I’m getting it pretty well. I can write a recursive procedure from scratch, but I know there are some things that I have to learn.
  • [1] Why is Sam asking these questions? Recursion is more obvious than conditionals.

Let’s try a sample recursive procedure written from scratch.

; Remove the first n elements of a list
(define list-drop
  (lambda (n lst)
    ; Base case: What's easy?  Dropping nothing from a list
    (if (zero? n)
        lst
        lst)))

Let’s try thinking about it conceptually on another problem (no code, just base case and recursive step).

I have a list of values, lst, and a number, n, and I want to break lst up into a list of n approximately-equal-length lists.

> (break-up '(a b c d e) 2)
'((a b c) (d e))
> (break-up '(a b c d e f g h) 4)
'((a b) (c d) (e f) (g h))
> (break-up '(a b c d e f g h) 2)
'((a b c d) (e f g h))
> (break-up '(a b c d e f g h) 3)
'((a b c) (d e f) (g h))
> (break-up '(a b c d e f g h) 1)
'((a b c d e f g h))
  • What’s a natural base case? When n is 1, we can just return a list of the original list.
  • How do lst and n change when you recurse? “Just throw something out there.”
    • Divide the length of the list by n. That tells you how big the first chunk should be. (The length of some of the sublists.) Round up. We’ll call that chunk-size.
    • We’ll set aside the first chunk-size elements.
    • We’ll then recurse after dropping chunk-size elements and n-1.
  • Note: Precondition: n>=1
  • Sam attempts an example all the way through
    • (break-up '(a b c d e f g h i) 4)
    • divide length by 4, that gives us 2.25, we round up to 3.
    • (cons '(a b c) (break-up '(d e f g h i) 3))
    • divide length (6) by 3. That gives us 2. We round up to 2.
    • (cons '(a b c) (cons '(d e) (break-up '(f g h i) 2)))
    • (cons '(a b c) (cons '(d e) (cons '(f g) (break-up '(h i) 1))))
    • (cons '(a b c) (cons '(d e) (cons '(f g) '((h i)))))
    • (cons '(a b c) (cons '(d e) '((f g) (h i))))

Questions

Quiz!

Representing lists with pairs and cons cells

  • See whiteboard.

Lab

Writeup #1 and #3.

Debrief

  • Are there lists that are not pairs? Yes. null is a list, but not a pair.
  • Are there pairs that are not lists? Yes. (cons 'a 'a) is a pair, but not a list.