Know how to create and work with dotted pairs using
cons
, car
, cdr
, and
cadr
and friends
Know how create and interact with lists, including the
list
function
Be able to implement simple recursive list processing functions in Racket
Know how to create recursive helper methods to carry information through the recursion
L02 Exercise B.1?
Scheme evaluates everything it sees. Special types like numbers, strings, booleans, etc. evaluate to themselves.
Except in special cases, when Scheme sees (stuff)
,
it assumes that it’s a procedure call and tries to evaluate it.
(1) ; errors
Exceptions so far - “special forms”:
lambda
, let
, if
,
cond
(cons 1 4)
Aside: quoting
quote
: please don’t evaluate this right now(1 . 4)
errors(quote (1 . 4))
does not error'(1 . 4)
Back to dotted pairs:
(define a (cons 1 4))
(define b (cons (cons 1 2) 3))
Play with car
and cdr
Show caar
, cdar
.
Do Exercises Part A
(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
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.