问题
I am quite new to racket, and I wondered if there is a way to combine two foldr` functions:
'(foldr + 0 (list 1 2 3 4))
;; output = 10 -> (4+(3+(2+(1+0))))
(foldr * 1 (list 1 2 3 4))
;; output = 24 -> (4*(3*(2*(1*0))))'
I want to receive this output : output = 64 -> (4+4∗(3+3∗(2+2∗(1+1∗0))))
回答1:
#|
(4*(3*(2*(1*0)))) -> 0 (not 24)
(4+(3+(2+(1+0)))) -> 10
(foldr + 0 (list 1 2 3 4))
->
(+ 1 (+ 2 (+ 3 (+ 4 0))))
->
10
(foldr * 1 (list 1 2 3 4))
->
(* 1 (* 2 (* 3 (* 4 1))))
->
24
if you want this
(4+4∗(3+3∗(2+2∗(1+1∗0))))
->
64
|#
(foldr (lambda (x y) (+ (* x y) x)) 0 '(4 3 2 1))
来源:https://stackoverflow.com/questions/59178550/how-do-i-combine-two-foldr-foldl-functions-for-this-output-racket-scheme