Is there builtin or idiomatic way in Haskell to "unpack" the elements of a list and treat them as individual arguments to a function?
For example, if I have f :: a -> b -> c -> d -> e
is there something compact like
f (unlist x)
accomplishes
let x = [1,2,3,4] in f (x!!0) (x!!1) (x!!2) (x!!3)
or at least a less "shouty" (too many !!
repetitions) way to unpack a list of known length in general (so that it can be used as arguments to a function, in this case).
Essentially what I'm looking for is something like what Sequence@@
does in Mathematica:
f[Sequence@@{1, 2, 3, 4}]
It wouldn't be particularly useful in Haskell type system:
Given this, use of tuples makes more sense than lists, as it avoids both problems; the standard library includes uncurry
which does this for functions of 2 arguments, and you could define uncurry3
, etc. by analogy:
uncurry3 :: (a -> b -> c -> d) -> ((a, b, c) -> d)
uncurry3 f (a, b, c) = f a b c
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With