該問題要求編寫滿足以下交互的forEach函數。它表示函數應該在列表和行爲周圍創建閉包。針對每個功能的方案
(define iterator (forEach (list 1 2 3 4 5)))
(iterator (lambda (x)(* x x))) → (1 4 9 16 25)
(iterator (lambda (x)(* x 10))) → (10 20 30 40 50)
我似乎無法弄清楚。以下是我已經開始但我可能是遙遠:
(define (map procedure items)
(if (null? items)
'()
(cons (procedure (car items))
(map procedure (cdr items)))))
(define (forEach l) (lambda (x)
(cond ((not (null? l))
(x (car l))
(forEach (map x (cdr l)))))))
(define iterator (forEach (list 1 2 3 4 5)))
(iterator (lambda (x)(* x x)))
(iterator (lambda (x)(* x 10)))