[英]What does `~` mean in Haskell?
I'm studying the mtl
library and trying to do some MonadTransformers of my own.我正在研究
mtl
库并尝试做一些我自己的 MonadTransformers。 I was checking the Control.Monad.State.StateT
declaration, and across all the code, I see this syntax:我正在检查
Control.Monad.State.StateT
声明,在所有代码中,我看到以下语法:
execStateT :: (Monad m) => StateT s m a -> s -> m s
execStateT m s = do
~(_, s') <- runStateT m s
return s'
What does this ~
operand mean?这个
~
操作数是什么意思?
This is the notation for a lazy pattern in Haskell.这是 Haskell 中惰性模式的表示法。 I can't say that I'm familiar with it but from here :
我不能说我熟悉它,但从这里开始:
It is called a lazy pattern, and has the form ~pat.
它被称为懒惰模式,格式为 ~pat。 Lazy patterns are irrefutable: matching a value v against ~pat always succeeds, regardless of pat.
惰性模式是无可辩驳的:不管 pat 是什么,匹配一个值 v 与 ~pat 总是成功的。 Operationally speaking, if an identifier in pat is later "used" on the right-hand-side, it will be bound to that portion of the value that would result if v were to successfully match pat, and ⊥ otherwise.
从操作上讲,如果 pat 中的标识符稍后在右侧“使用”,则它将绑定到值的那部分,如果 v 成功匹配 pat,则为 ⊥ 否则。
Also, this section may be useful.此外, 本节可能有用。
For a normal pattern match, the value that should be matched needs to be evaluated, so that it can be compared against the pattern.对于正常的模式匹配,需要评估应该匹配的值,以便可以将其与模式进行比较。
~
denotes a lazy pattern match: It is just assumed that the value will match the pattern. ~
表示惰性模式匹配:只是假设该值将匹配模式。 The match is then only done later, if the value of a matched variable is actually used.如果实际使用了匹配变量的值,则匹配仅在稍后完成。
It's equivalent to它相当于
execStateT m s = do
r <- runStateT m s
return (snd r)
or或者
execStateT m s =
runStateT m s >>= return . snd
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.