gpt4 book ai didi

haskell - '@' 在 Haskell 中是什么意思?

转载 作者:行者123 更新时间:2023-12-04 05:23:30 25 4
gpt4 key购买 nike

我试过用谷歌搜索,但结果很短。我通过阅读一些文章来加深我的 Haskell 知识,我遇到了一篇使用我以前从未见过的语法的文章。
一个例子是:

reconstruct node@(Node a b c l r) parent@(Node b d le ri)

我以前从未见过这些@。我尝试在网上搜索答案,但没有找到答案。这只是一种嵌入标签以帮助使事情更清晰的方法,还是它们对代码有实际影响?

最佳答案

它用于模式匹配。现在node变量将引用整个 Node参数 Node a b c l r 的数据类型.因此,不要以 Node a b c l r 的形式传递给函数, 你可以使用 node而不是传递它。

一个更简单的示例来演示它:

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

看看第一个版本有多简单?

关于haskell - '@' 在 Haskell 中是什么意思?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30326249/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com