Still a Haskell newbie here. I know just enough to get myself into trouble with wrong assumptions. If I have the following function...
quadsum w x y z = w+x+y+z
I want a function that can take a list, use each element as a parameter in a specified function like quadsum
, and return a curried function for later use.
I've been trying something to the effect of...
magicalFunctionMaker f [] = (f)
magicalFunctionMaker f (x:xs) = magicalFunctionMaker (f x) xs
With the hope of being able to do this...
magicalFunctionMaker (quadsum) [4,3,2]
Getting a curried function like...:
(((quadsum 4) 3) 2)
Or, alternatively, call:
magicalFunctionMaker (quadsum) [4,3,2,1]
Resulting in...
((((quadsum 4) 3) 2) 1)
Is this possible? How misguided am I?
I think you are misunderstanding the Haskell type system.
First of all, your "quadsum" function is curried already. You can write "quadsum 4 3" and get back a function that expects the 2 and the 1 as extra arguments. When you write "quadsum 4 3 2 1" that is equivalent to "((((quadsum 4) 3) 2) 1)".
In Haskell a list of integers has a different type to an integer or a tuple such as "(4,3,2,1)". Given this, it is rather difficult to understand what you are trying to do. What is supposed to happen if you write this?
magicalFunctionMaker quadsum [5,4,3,2,1]
Your "magicalFunctionMaker" looks rather like "foldl", except that the function you give to foldl only takes two arguments. So you can write:
mySum = foldl (+) 0
This returns a function that takes a list and sums the elements.
(BTW, once you grok this, learn about the difference between foldl and foldr.
Edit:
Having re-read your question, I think you are trying to get:
magicalFunctionMaker quadSum [4,3,2,1] :: Integer
magicalFunctionMaker quadSum [4,3,2] :: Integer -> Integer
This is impossible because the type of magicalFunctionMaker would depend on the length of the list argument, which would imply dynamic typing. As someone said, polyvariadic functions can do something approaching this (although not with a list argument), but that requires quite a few milli-olegs of type hackery.