apply function arguments in a list

If I have a list containing the arguments I want to give to a function, is there a general way to supply those arguments in a compact syntax? In other words, I could have args = [1,2,3] f x y z = ... I would write t = f (args!!0) (args!!1) (args!!2) but there may be a neater, more general syntax. I tried writing a fold with $, but I don't think that works because the type of each step is different. I.e. f :: a -> a -> a -> a f x :: a -> a -> a f x y :: a -> a f x y z :: a This would seem to preclude a general way of writing a function that supplies arguments from a list.

If I have a list containing the arguments I want to give to a function, is there a general way to supply those arguments in a compact syntax?
In other words, I could have
args = [1,2,3] f x y z = ...
I would write
t = f (args!!0) (args!!1) (args!!2)
but there may be a neater, more general syntax. In general, the problem is that you can't guarantee ahead of time that
Michael Mossey wrote: the list will have three elements, so however you write it, you'll turn a compile-time error (wrong number of args to function) into a run-time error (!! says index is invalid). The neatest way is probably: t [x,y,z] = f x y z Which will give a slightly better error if the list is the wrong size. If your function takes many arguments of the same type, perhaps you could generalise it to take a list of arbitrary size, and do away with requiring three arguments? Alternatively, use tuples if you want an easy way to carry the arguments around together: args = (1, 2, 3) f x y z = ... uncurry3 f (x,y,z) = f x y z t = uncurry3 f (uncurry already exists, I think uncurry3 is one you have to add yourself). Neil.
participants (3)
-
Michael Mossey
-
Miguel Mitrofanov
-
Neil Brown