“@"是什么意思?符号是指参考 Haskell 中的列表吗?
我遇到了一段如下所示的 Haskell 代码:
I've come across a piece of Haskell code that looks like this:
ps@(p:pt)
@
符号在这种情况下是什么意思?我似乎在 Google 上找不到任何信息(遗憾的是很难在 Google 上搜索符号),而且我在 Prelude 文档中找不到该函数,所以我想它一定是某种语法糖.
What does the @
symbol mean in this context? I can't seem to find any info on Google (it's unfortunately hard to search for symbols on Google), and I can't find the function in the Prelude documentation, so I imagine it must be some sort of syntactic sugar instead.
是的,这只是语法糖,@
朗读为as".ps@(p:pt)
为您提供
Yes, it's just syntactic sugar, with @
read aloud as "as". ps@(p:pt)
gives you names for
- 列表:
ps
- 列表的头部:
p
- 列表的尾部:
pt
如果没有 @
,您必须在 (1) 或 (2):(3) 之间进行选择.
Without the @
, you'd have to choose between (1) or (2):(3).
这个语法实际上适用于任何构造函数;如果您有 data Tree a = Tree a [Tree a]
,则 t@(Tree _ kids)
可让您访问树及其子树.
This syntax actually works for any constructor; if you have data Tree a = Tree a [Tree a]
, then t@(Tree _ kids)
gives you access to both the tree and its children.