map 的原始定义
(define (map p sequence)
(if (null? sequence)
'()
(cons (p (car sequence))
(map p (cdr sequence)))))
accumulate 的定义
(define (accumulate op initial sequence)
(if (null? sequence)
initial
(op (car sequence)
(accumulate op initial (cdr sequence)))))
原题
(define (map p sequence)
(accumulate (lambda (x y) <???>) nil sequence))
将原题中的 accumulate 用定义daihuan代换 (lambda (x y) <???>) => op nil => initial sequence => sequence
得到下面这段代码
(define (map p sequence)
(if (null? sequence)
'()
((lambda (x y) <???>) (car sequence)
(accumulate (lambda (x y) <??>) '() sequence))))
通过将这个展开式和原本 map 的定义对比可以看出,我们只要让 (lambda (x y) <??>) 中的 <??> 的作用等同于 (cons (p x) y) 即可,因此,这个答案的解为 (lambda (xy) (cons (p x) y)) 。
以下是完整定义:
(define (map p sequence)
(accumulate (lambda (x y)
(cons (p x) y))
'()
sequence))