class 7: programming with lists

19
Class 7: Programming with Lists cs1120 Fall 2011 David Evans 7 September No notes for today – continue on Class 6 No

Upload: david-evans

Post on 20-Nov-2014

526 views

Category:

Technology


1 download

DESCRIPTION

Making cons, car, and cdrThreesomes, Quadruples, and More!ListsGold Star Grading Scale

TRANSCRIPT

Page 1: Class 7: Programming with Lists

Class 7: Programming with Lists

cs1120 Fall 2011David Evans7 September 2011 No notes for today – continue on Class 6 Notes

Page 2: Class 7: Programming with Lists

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.

Page 3: Class 7: Programming with Lists

3

Implementing cons, car and cdrRemember pick-one:(define pick-one (lambda p a b) (if p a b)))

Page 4: Class 7: Programming with Lists

4

Pairs are fine, but how do we make threesomes?

xkcd (Creative Commons)

Page 5: Class 7: Programming with Lists

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)))

Page 6: Class 7: Programming with Lists

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)))

Page 7: Class 7: Programming with Lists

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 …?

Page 8: Class 7: Programming with Lists

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!

Page 9: Class 7: Programming with 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!

Page 10: Class 7: Programming with Lists

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!

Page 11: Class 7: Programming with Lists

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

Page 12: Class 7: Programming with Lists

12

List Examples> null()> (cons 1 null)(1)> (list? null)#t> (list? (cons 1 2))#f> (list? (cons 1 null))#t

Page 13: Class 7: Programming with Lists

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

Page 14: Class 7: Programming with Lists

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

Page 15: Class 7: Programming with Lists

Quiz / Returning PS1

Page 16: Class 7: Programming with Lists

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

Page 17: Class 7: Programming with Lists

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.

Page 18: Class 7: Programming with Lists

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)

Page 19: Class 7: Programming with Lists

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.