|
University of Virginia Computer Science CS150: Computer Science, Fall 2005 |
(none) 7 September 2005 |
(define (listproc lst)
(if (null? lst)
[[[ insert base case here ]]]
([[[ f ]]] (car lst) (listproc (cdr lst)))))
For example:
(define (sumlist lst)
(if (null? lst)
0
(+ (car lst) (sumlist (cdr lst)))))
(define (insertl lst f stopval)
(if (null? lst)
stopval
(f (car lst)
(insertl (cdr lst) f stopval))))
(define (sumlist lst) (insertl lst + 0))
(define (productlist lst) (insertl lst * 1))
(define (length lst)
(insertl lst (lambda (head rest) (+ 1 rest)) 0))
(define (remove cf lst) ;; operands: cf is a comparison function that takes one operand and ;; evaluates to #t or #f ;; lst is a list of values ;; result: evaluates to a list containing the only elements of lst for ;; which (cfCould you define remove using just a map application? (Explain how or why it is impossible.)) evaluates to #f (if (null? lst) _________________________ (if (cf (car lst)) ______________________________ ;; discard first element ______________________________))) ;; keep first element
Could you define remove using just an insertl application?
(Explain how or why it is impossible.)
(define (filter cf lst) ;; operands: cf is a comparison function that takes one operand and ;; evaluates to #t or #f ;; lst is a list of values ;; result: evaluates to a list containing the only elements of lst for ;; which (cfThere are many ways to define filter. Try to find the shortest one. You can use anything we have already defined.) evaluates to #t )
Challenge: Define a procedure (intsto n) that evaluates to a list containing the integers less than or equal to n in order. For example, (intsto 3) ==> (1 2 3) and (intsto 0) ==> (). This is pretty tricky. Try your definition in DrScheme.
In mathematics you don't understand things, you just get used to them.
John Von Neumann
| "); print ( $res[$first] ) ; print (" |
|
CS 150: Computer Science University of Virginia |
evans@virginia.edu Using these Materials |