How do I combine two foldr/ foldl functions for this output? (Racket/Scheme)

99封情书 提交于 2019-12-24 19:24:25

问题


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

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!