Haskell中的$ mean /做了什么? [英] What does $ mean/do in Haskell?
本文介绍了Haskell中的$ mean /做了什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
在编写稍微复杂的函数时,我注意到 $
被使用了很多,但我不知道它的作用。
解决方案
是中缀application。它被定义为
($)::(a - > b) - > ($ -
$ bf $ x = fx
- 或
($)fx = fx
- 或
($) = id
这对避免多余的括号很有用: f(gx)== f $ gx
。
特别有用的位置是尾随lambda体,如$ / b
$ b
forM_ [1..10] $ \i - > do
l < - readLine
replicateM_ i $ print l
与
forM_ [1..10](\i- - > do
l < - readLine
replicateM_我(打印l)
)
或者,表达应用这个参数到任何函数
applyArg :: a - > (a - > b) - > b
applyArg x =($ x)
>>>地图($ 10)[(+1),(+2),(+3)]
[11,12,13]
When you are writing slightly more complex functions I notice that $
is used a lot but I don't have a clue what it does?
解决方案
$
is infix "application". It's defined as
($) :: (a -> b) -> (a -> b)
f $ x = f x
-- or
($) f x = f x
-- or
($) = id
It's useful for avoiding extra parentheses: f (g x) == f $ g x
.
A particularly useful location for it is for a "trailing lambda body" like
forM_ [1..10] $ \i -> do
l <- readLine
replicateM_ i $ print l
compared to
forM_ [1..10] (\i -> do
l <- readLine
replicateM_ i (print l)
)
Or, trickily, it shows up sectioned sometimes when expressing "apply this argument to whatever function"
applyArg :: a -> (a -> b) -> b
applyArg x = ($ x)
>>> map ($ 10) [(+1), (+2), (+3)]
[11, 12, 13]
这篇关于Haskell中的$ mean /做了什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文