问题描述
我尝试使用Google谷歌搜索,但失败了.我通过阅读一些文章来增强我的Haskell知识,并且偶然发现了一种使用以前从未见过的语法的文章.一个例子是:
I've tried googling but come up short. I am furthering my Haskell knowledge by reading some articles and I came across one that uses a syntax I've never seen before.An example would be:
reconstruct node@(Node a b c l r) parent@(Node b d le ri)
我以前从未见过这些@.我尝试在网上搜索答案,但结果很短.这仅仅是嵌入标签以帮助使事情变得更清晰的一种方法,还是对代码有实际影响?
I've never seen these @'s before. I tried searching online for an answer but came up short. Is this simply a way to embed tags to help make things clearer, or do they have an actual impact on the code?
推荐答案
它用于模式匹配.现在, node
变量将引用参数 Node a b c l r
的整个 Node
数据类型.因此,您可以使用 node
代替将其传递给 Node a b c l r
作为功能,而不是传递给该函数.
It is used in pattern matching. Now node
variable will refer to the entire Node
data type for the argument Node a b c l r
. So instead of passing to the function as Node a b c l r
, you can use node
instead to pass it up.
一个简单得多的示例来演示它:
A much simpler example to demonstrate it:
data SomeType = Leaf Int Int Int | Nil deriving Show
someFunction :: SomeType -> SomeType
someFunction leaf@(Leaf _ _ _) = leaf
someFunction Nil = Leaf 0 0 0
someFunction
也可以写为:
someFunction :: SomeType -> SomeType
someFunction (Leaf x y z) = Leaf x y z
someFunction Nil = Leaf 0 0 0
看看第一个版本有多简单?
See how simpler was the first version ?
这篇关于在Haskell中,"@"是什么意思?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!