class 7: programming with lists
DESCRIPTION
Making cons, car, and cdrThreesomes, Quadruples, and More!ListsGold Star Grading ScaleTRANSCRIPT
Class 7: Programming with Lists
cs1120 Fall 2011David Evans7 September 2011 No notes for today – continue on Class 6 Notes
Pairs Recap
(cons a b)Evaluates to a Pair containing a and b.
(car p)If p is a Pair, evaluates to the first part of p.Otherwise, error.
(cdr p)If p is a Pair, evaluates to the second part of p.Otherwise, error.
3
Implementing cons, car and cdrRemember pick-one:(define pick-one (lambda p a b) (if p a b)))
4
Pairs are fine, but how do we make threesomes?
xkcd (Creative Commons)
5
Triple
A Triple is just a Pair where one of the parts is a Pair!
(define (triple a b c) (cons a (cons b c)))(define (t-first t) (car t))(define (t-second t) (car (cdr t))) (define (t-third t) (cdr (cdr t)))
6
Quadruple
A Quadruple is a Pair where the second part is a Triple
(define (quadruple a b c d) (cons a (triple b c d)))(define (q-first q) (car q))(define (q-second q) (t-first (cdr t))) (define (q-third t) (t-second (cdr t)))(define (q-fourth t) (t-third (cdr t)))
7
Multuples
A Quintuple is a Pair where the second part is a Quadruple
A Sextuple is a Pair where the second part is a Quintuple
A Septuple is a pair where the second part is a Sextuple
An Octuple is group of octupiA ? is a Pair where the second part is a …?
8
Lists
List ::= (cons Element List)
A List is a Pair where the second part is a List.
One big problem: how do we stop?This only allows infinitely long lists!
9
Lists
List ::= (cons Element List)List ::=
A List is either: a Pair where the second part is a Listor, empty
It’s hard to write this!
10
Null
List ::= (cons Element List)List ::=
A List is either: a Pair where the second part is a Listor, empty (null)
null
null for Lists is like for BNF grammars!
11
Recap
A List is either: a Pair where the second part is a List
or null Pair primitives:
(cons a b) Construct a pair <a, b>(car pair) First part of a pair(cdr pair) Second part of a pair
12
List Examples> null()> (cons 1 null)(1)> (list? null)#t> (list? (cons 1 2))#f> (list? (cons 1 null))#t
13
More List Examples
> (list? (cons 1 (cons 2 null)))#t> (car (cons 1 (cons 2 null)))1> (cdr (cons 1 (cons 2 null)))(2)
Jump to Quiz
14
list-count-trues
Define a procedure that takes as input a list, and produces as output the number of non-false values in the list.
(list-count-trues null) 0• (list-count-trues (list 1 2 3)) 3• (list-count-trues (list false (list 2 3 4))) 1
Quiz / Returning PS1
Problem Sets
Unlike the quiz, mostly for learning not grading
You should read the PS1 Comments on the course site (even if you think you got everything on PS1)Will be posted later todayCome to office hours with any questions
17
Problem Sets Grading Scale
Gold Star – Excellent Work. You got everything I wanted out of this assignment.
Green Star – You got most things on this PS, but missed some things I expected.
Silver Star – Some serious problems.Red Star – Didn’t get much of what you
should have out of this problem set.
PS1 Average: What you turn in is usually more important than passing the automatic tests.
18
No upper limit
- Double Gold Star: exceptional work! Better than I expected anyone would do.
- Triple Gold Star: Better than I thought possible (e.g., moviemosaic for PS1)
- Quadruple Gold Star: You have broken important new ground in CS which should be published in a major conference! (not yet ever awarded in cs1120)
- Quintuple Gold Star: You deserve to win a Turing Award! (a fast, general way to make the best non-repeating photomosaic on PS1, or a proof that it is impossible)
QuizFront side: graded (no gold stars, just points)
Unlike almost everything else in this class, this is only designed for grading. No thinking is expected or necessary!
Back side: ungradedCourse feedbackChallenging questions
From course pledge:I will provide useful feedback. I realize that cs1120 is an evolving course and it is important that I let the course staff know what they need to improve the course. I will not wait until the end of the course to make the course staff aware of any problems. I will provide feedback either anonymously or by contacting the course staff directly. I will fill out all course evaluation surveys honestly and thoroughly.