[an error occurred while processing this directive]
Class 5 Notes
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
the list.
- Combine the base case and the recursive case to solve the problem.
List?
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)
______________________________________
________
)))
Sumlist
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)
)
Map
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.
Examples:
> (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)))
current-deck)))
What is this expression doing?
Tracing
To enable tracing, evaluate (require (lib
"trace.ss")). (You can put this in your definitions file, like it
is in poker.scm.) Then,
(trace procedure)
will enable tracing of the procedure. The interpreter will print out a
message corresponding to the entrance (showing the parameters) and exit
(showing the resulting value) of the traced procedure. This is a useful
tool for understanding what is going on when your programs are
evaluated.
Links
- That "huge saw machine" is a bucket-wheel excavator,
a mobile strip-mining machine. Bucket-wheel excavators are among
the largest vehicles ever constructed, and the largest terrestrial vehicle
in human history is a bucket-wheel excavator.
[an error occurred while processing this directive]