[Haskell-cafe] Increasing number of parameters
jacek.generowicz at cern.ch
Fri Oct 15 09:40:06 EDT 2010
On 2010 Oct 15, at 11:38, Jacek Generowicz wrote:
> So I'm trying to get to grips with a simpler variation on the same
> theme, and I'm still failing. I'm trying to write something along
> the lines of
> addArg :: nArgFn -> a -> nPlus1ArgFn
> addArg fn a = (a+) <---- fn where
> <---- = something which applies its right parameter to however
> many arguments it needs and feeds the result to the left parameter
> in order to allow me to say
> sum2 = (+)
> sum3 = addArg sum2
> sum4 = addArg sum3
-- OK, I've understood.
-- You use an accumulator to keep track of what has been done with the
-- arguments that have been seen so far, and addArg takes one more
-- argument, each time, and mixes it in with what is already there.
-- I smell a monad.
addArgSum :: (Num a) => (a -> t) -> a -> a -> t
addArgSum fn acc arg = fn (acc + arg)
sum1' = id
sum2' = addArgSum sum1'
sum3' = addArgSum sum2'
-- And here's a more general version.
addArg combine fn acc arg = fn (combine arg acc)
sum1 = id
sum2 = addArg (+) sum1
sum3 = addArg (+) sum2
sum4 = addArg (+) sum3
-- But I don't really get why the following leads to complaints about
-- infinite types.
-- sumN n = iterate (addArg (+)) id
More information about the Haskell-Cafe