CSCI 301 L03 Notes

Lecture 3 - Notes

Outcomes:

Racket 2

Announcements

Outline

L02 Exercise B.1?

Evaluation

(1) ; errors

Exceptions so far - “special forms”:

Dotted pairs

(cons 1 4)

Aside: quoting

Back to dotted pairs:

(define a (cons 1 4))

(define b (cons (cons 1 2) 3))

Do Exercises Part A

Lists

(define c (cons 1 (cons 2 (cons 3 4))))

(define lst (cons 1 (cons 2 (cons 3 (cons 4 '())))))

'() is the empty list

Recursively defined list. A list is:

c above is not a “proper list”, whereas d is.

Shorthand: (list 1 2 3 4)

(equal? d (list 1 2 3 4)) ; these are the same!

Again, notice: (1 2 3 4) ; by itself errors because 1 isn't a procedure

(reverse (1 2)) ; errors

(reverse '(1 2)) or (reverse (list 1 2)) has the intended effect

Do Exercises Part B

Recursive Solutions

Reminder: base case, recursive case

A natural fit for recursively defined structures like lists!

Example: length of a list, recursively.

Do Exercises Part C

Recursive helpers: Example - compute the average in one pass.