[an error occurred while processing this directive]
cs150: Notes 7
- (extended from 29 January) Before Monday, 5 February: Read GEB, Little Harmonic Labyrinth and GEB, Chapter 5
- Friday, 2 February (beginning of class): Problem Set 2. (Note: Problem
Set 2 will be accepted without penalty or any permission required until
the beginning of class on Monday, 5 February, as long as you promise to
still finish reading GEB Chapter 5 before Monday's class.)
List Procedures Practice
- Be very optimistic! Since lists themselves are recursive data
structures, most problems involving lists can be solved with recursive procedures.
- Think of the simplest version of the problem, something you can
already solve. This is the base case. For lists, this is usually when
the list is null.
- Consider how you would solve a big version of the problem by using
the result for a slightly smaller version of the problem. This is the
recursive case. For lists, the smaller version of the problem is the rest (cdr) of
- Combine the base case and the recursive case to solve the problem.
Define a procedure list? that takes one operand and
evaluates to #t if the operand is a list, and #f otherwise.
- What should we do when the input is null?
- When the input is a cons pair (the built-in procedure
pair? evaluates to #t if and only if its input is a
cons pair), what should we do?
- When the input is not null and not a cons what
should we do?
(define (list? p)
(if (null? p)
(if (pair? p)
Define a procedure sumlist that takes a list as its operand and
evaluates to the sum of elements in the list. For example, (sumlist
(list 1 2 3)) should evaluate to 6, and (sumlist null)
should evaluate to 0.
(define (sumlist p)
Define a procedure map that takes a procedure as its first
operand and a list as its second operand and evaluates to the list
containing the values resulting from applying the procedure to each
element of the input list.
> (map car (list (cons 1 2) (cons 2 3)))
> (map + null)
> (map (lambda (x) (* x x)) (list 1 2 3))
(1 4 9)
> (map (lambda (el) (> el 3)) (list 2 4 6))
> (map (lambda (el) (+ 1 el)) (list 1 2 3))
(define (map f lst)
The analyze-flop-situation proceudre from ps2 uses map:
(map (lambda (turn-card)
(analyze-turn-situation hole1 hole2 (cons turn-card community)))
What is this expression doing?