現在,我一直在嘗試實施累積功能幾個星期。我已經正確地實現了一個「Map」函數,它遍歷一個列表並在每個元素上運行一個函數。在計劃中實施「收集」功能
我使用這個功能來實現「收集」
(define accumulate
(lambda (op base func ls)
(if(null? ls)
ls
(cond (not (null? (cdr ls)) (op base (map func ls) (accumulate op base func (cdr ls))))
(op base (map func ls) (op base (func(car ls))))
)
)))
;It gets to a point (the last element) after applying the map function to each element,
;where it is '(number) instead of an expected "number" (outside of()). I cannot figure out
;how to circumvent this.
我卡在如何得到這個權利。什麼是正確的方法來做到這一點?
預期的結果是:
; accumulate: combines using OP the values of a list LS after mapping a function FUNC on it
; (accumulate + 0 sqr '(1 2 3)) => 14
; (accumulate * 1 sqr '(1 2 3)) => 36
;
我認爲你的'cond'語句不正確。 '(cond((not(null?(cdr ls)))(op base ...' – chepner
'accumulate'的輸出應該是什麼?請提供一個帶有預期輸出的示例輸入 –
另外,爲什麼你需要'map'嗎?你確定你的輸入是_lists_的列表嗎? –