Fold is an extension of Nest for 2 arguments. How does one extend this concept to multiple arguments. Here is a trivial example:
FoldList[#1 (1 + #2) &, 1000, {.01, .02, .03}]
Say I want do something like:
FoldList[#1(1+#2)-#3&,1000,{.01,.02,.03},{100,200,300}]
where 100,200,300 are the values for #3. I know Fold doesn't work this way. I'm looking for an approach that will work like this... ((1000*(1.01)-100)*1.02-200)*1.03-300).
Is there a way to extend Fold to more than 2 arguments? or is there a better approach for solving problems like this?
Answer
Yes, there is. Group your extra arguments in a list, and address them by their positions in the function under Fold
. For your particular example:
FoldList[#1 (1 + First@#2) - Last@#2 &, 1000, Transpose@{{.01, .02, .03}, {100, 200, 300}}]
(* {1000, 910., 728.2, 450.046} *)
Comments
Post a Comment