私は数週間、累積関数を実装しようとしています。私は適切に "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'ステートメントが正しくないと思います。 – chepner
正確には 'accumulate'の出力であると想定されていますか?期待される出力をサンプル入力してください –
また、なぜそうするのですか?(cond((not(null?(cdr ls))) 'map'が必要ですか?あなたの入力は_lists_のリストであることを確信していますか? –