Does Haskell have a splat operator like Python and

2019-02-08 12:03发布

问题:

In Python and Ruby (and others as well, I'm sure). you can prefix an enumerable with * ("splat") to use it as an argument list. For instance, in Python:

>>> def foo(a,b): return a + b
>>> foo(1,2)
3
>>> tup = (1,2)
>>> foo(*tup)
3

Is there something similar in Haskell? I assume it wouldn't work on lists due to their arbitrary length, but I feel that with tuples it ought to work. Here's an example of what I'd like:

ghci> let f a b = a + b
ghci> :t f
f :: Num a => a -> a -> a
ghci> f 1 2
3
ghci> let tuple = (1,2)

I'm looking for an operator (or function) that allows me to do:

ghci> f `op` tuple
3

I have seen (<*>) being called "splat", but it doesn't seem to be referring to the same thing as splat in other languages. I tried it anyway:

ghci> import Control.Applicative
ghci> f <*> tuple

<interactive>:1:7:
    Couldn't match expected type `b0 -> b0'
                with actual type `(Integer, Integer)'
    In the second argument of `(<*>)', namely `tuple'
    In the expression: f <*> tuple
    In an equation for `it': it = f <*> tuple

回答1:

Yes, you can apply functions to tuples, using the tuple package. Check out, in particular, the uncurryN function, which handles up to 32-tuples:

Prelude Data.Tuple.Curry> (+) `uncurryN` (1, 2)
3


回答2:

The uncurry function turns a function on two arguments into a function on a tuple. Lists would not work in general because of their requirement for homogeneity.



回答3:

No, Haskell's type system doesn't like that. Check this similar question for more details:

How do I define Lisp’s apply in Haskell?

BTW, the splat operator you talk about is also known as the apply function, commonly found on dynamical functional languages (like LISP and Javascript).