Whilst foldl
may only apply one function, you can have different functions in your data. The trick it to attach the appropriate +
, -
or *
function to your data. You're off to the right start:
funclist = [(+), (-), (*)]
But lets now make a infinite version of the above list, like [(+), (-), (*), (+), (-), (*)...]
infinite_funclist = cycle funclist
Lets assign the numbers we're going to fold over here. first
is in this case 65
, rest
is [66..70]
(first:rest) = [65..70]
Now we zip together rest
and infinite_funclist
to get [(66,(+)), (67,(-)), (68,(*)), (69,(+)), (70,(-))]
. We start with first
, and for every new element, we apply the operation that's in the second part of the current tuple to the current value and the first part like so:
result = foldl' (\acc (v, f) -> acc `f` v) first (zip rest infinite_funclist)
If we want to print the result we can do so like so:
main = print result
(Link to code here)