程序代写代做代考 CSSE 304 Day 10

CSSE 304 Day 10
Procedural Abstraction
Four similar recursive procedures (do them on the board)
 (define list-sum
(lambda (ls) …) ; sum of a list-of-numbers;
 (define list-prod
(lambda (ls) …) ; product of a list-of-numbers;
 (define apply-to-all ; curried version of map (lambda (proc) (lambda (ls) … )
 (define member?-c ; is item a member of ls? (lambda (item) (lambda ls) …)
What do the four procedures have in common? What are their differences? Can we abstract the recursion pattern, creating a procedure that makes list-recursion procedures?
1

list-recur abstracts list-recursion
 A procedure that returns a procedure that recurs down a list.
(define list-recur
(lambda (base-value list-proc)
(letrec
([helper
(lambda (ls)
(if (null? ls)
base-value
For next class session, try to come up with an interesting example or two that can use list-recur
(list-proc (car ls)
(helper (cdr ls)))))])
helper)))
Use it to write our four procedures.
Use it to write length
Interlude
2

list-recur abstracts list recursion
Before the next class
 Try to come up with at least one other procedure that is easy to write using list-recur. Hopefully something that is not a clone of one that we wrote.
 And write it!
(define list-sum (list-recur 0 +) (define list-prod (list-recur 1 *)
(define apply-to-all
(lambda (proc)
(list-recur ′()
(lambda (x y)
(cons (proc x) y)))))
(define member-c?
(lambda (item)
(list-recur #f (lambda (x y)
(or (equal? item x)
y)))))
(define length (list-recur ;fill it in
)
Your examples?
One of these is a bad idea! Which one and why?
3