[an error occurred while processing this directive]

cs150: Notes 7

Assignments Due

List Procedures Practice

  1. Be very optimistic! Since lists themselves are recursive data structures, most problems involving lists can be solved with recursive procedures.
  2. 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.
  3. 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 the list.
  4. 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.

  1. What should we do when the input is null?

  2. 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?

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

(1 2)

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

cs1120: Computer Science
University of Virginia
Using these Materials