gfx*_*onk 15 haskell tuples splat
在Python和Ruby(以及其他人,我敢肯定).你可以在枚举前加上*("splat")来将它用作参数列表.例如,在Python中:
>>> def foo(a,b): return a + b
>>> foo(1,2)
3
>>> tup = (1,2)
>>> foo(*tup)
3
Run Code Online (Sandbox Code Playgroud)
Haskell中有类似的东西吗?我认为由于它们的任意长度它不适用于列表,但我觉得有了元组它应该工作.这是我想要的一个例子:
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)
Run Code Online (Sandbox Code Playgroud)
我正在寻找一个允许我这样做的操作符(或函数):
ghci> f `op` tuple
3
Run Code Online (Sandbox Code Playgroud)
我看到(<*>)被称为"splat",但它似乎并不是指与其他语言中的splat相同的东西.无论如何我试过了:
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
Run Code Online (Sandbox Code Playgroud)
Dan*_*ner 15
是的,您可以使用元组包将函数应用于元组.特别是检查uncurryN函数,它可以处理多达32个元组:
Prelude Data.Tuple.Curry> (+) `uncurryN` (1, 2)
3
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1436 次 |
| 最近记录: |