I've come across a piece of Haskell code that looks like this:
我遇到了一段看起来像这样的Haskell代码:
ps@(p:pt)
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.
在这种情况下,@符号意味着什么?我似乎无法在Google上找到任何信息(遗憾的是很难在Google上搜索符号),而且我在Prelude文档中找不到该功能,所以我想它必须是某种语法糖。
4 个解决方案
#1
Yes, it's just syntactic sugar, with @
read aloud as "as". ps@(p:pt)
gives you names for
是的,它只是语法糖,@朗读为“as”。 ps @(p:pt)给出了你的名字
- the list:
ps
- the list's head :
p
- the list's tail:
pt
清单:ps
列表的头部:p
列表的尾巴:pt
Without the @
, you'd have to choose between (1) or (2):(3).
没有@,你必须在(1)或(2):( 3)之间做出选择。
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.
这种语法实际上适用于任何构造函数;如果你有数据树a =树a [树a],那么t @(Tree _ kids)可以让你访问树及其子节点。
#2
The @
Symbol is used to both give a name to a parameter and match that parameter against a pattern that follows the @
. It's not specific to lists and can also be used with other data structures.
@符号用于为参数指定名称,并将该参数与@后面的模式匹配。它不是特定于列表,也可以与其他数据结构一起使用。
This is useful if you want to "decompose" a parameter into it's parts while still needing the parameter as a whole somewhere in your function. One example where this is the case is the tails
function from the standard library:
如果您想要将参数“分解”到它的部分,同时仍然需要在函数中的某个地方整个参数,这将非常有用。这种情况的一个例子是标准库中的tails函数:
tails :: [a] -> [[a]]
tails [] = [[]]
tails xxs@(_:xs) = xxs : tails xs
#3
I want to add that @
works at all levels, meaning you can do this:
我想补充说@在所有级别都有效,这意味着你可以这样做:
let a @ (b @ (Just c), Just d) = (Just 1, Just 2) in (a, b, c, d)
Which will then produce this: ((Just 1, Just 2), Just 1, 1, 2)
然后会产生这个:((只有1,只有2),只有1,1,2)
So basically it's a way for you to bind a pattern to a value. This also means that it works with any kind of pattern, not just lists, as demonstrated above. This is a very useful thing to know, as it means you can use it in many more cases.
所以基本上它是一种将模式绑定到值的方法。这也意味着它适用于任何类型的模式,而不仅仅是列表,如上所示。这是一个非常有用的东西,因为它意味着你可以在更多的情况下使用它。
In this case, a
is the entire Maybe Tuple
, b
is just the first Just
in the tuple, and c
and d
are the values contained in the first and second Just
in the tuple respectively
在这种情况下,a是整个Maybe Tuple,b只是第一个Just in the tuple,c和d分别包含在元组中的第一个和第二个Just中
#4
To add to what the other people have said, they are called as-patterns (in ML the syntax uses the keyword "as"), and are described in the section of the Haskell Report on patterns.
为了增加其他人所说的内容,它们被称为as-patterns(在ML中,语法使用关键字“as”),并在Haskell模式报告部分中进行了描述。
#1
Yes, it's just syntactic sugar, with @
read aloud as "as". ps@(p:pt)
gives you names for
是的,它只是语法糖,@朗读为“as”。 ps @(p:pt)给出了你的名字
- the list:
ps
- the list's head :
p
- the list's tail:
pt
清单:ps
列表的头部:p
列表的尾巴:pt
Without the @
, you'd have to choose between (1) or (2):(3).
没有@,你必须在(1)或(2):( 3)之间做出选择。
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.
这种语法实际上适用于任何构造函数;如果你有数据树a =树a [树a],那么t @(Tree _ kids)可以让你访问树及其子节点。
#2
The @
Symbol is used to both give a name to a parameter and match that parameter against a pattern that follows the @
. It's not specific to lists and can also be used with other data structures.
@符号用于为参数指定名称,并将该参数与@后面的模式匹配。它不是特定于列表,也可以与其他数据结构一起使用。
This is useful if you want to "decompose" a parameter into it's parts while still needing the parameter as a whole somewhere in your function. One example where this is the case is the tails
function from the standard library:
如果您想要将参数“分解”到它的部分,同时仍然需要在函数中的某个地方整个参数,这将非常有用。这种情况的一个例子是标准库中的tails函数:
tails :: [a] -> [[a]]
tails [] = [[]]
tails xxs@(_:xs) = xxs : tails xs
#3
I want to add that @
works at all levels, meaning you can do this:
我想补充说@在所有级别都有效,这意味着你可以这样做:
let a @ (b @ (Just c), Just d) = (Just 1, Just 2) in (a, b, c, d)
Which will then produce this: ((Just 1, Just 2), Just 1, 1, 2)
然后会产生这个:((只有1,只有2),只有1,1,2)
So basically it's a way for you to bind a pattern to a value. This also means that it works with any kind of pattern, not just lists, as demonstrated above. This is a very useful thing to know, as it means you can use it in many more cases.
所以基本上它是一种将模式绑定到值的方法。这也意味着它适用于任何类型的模式,而不仅仅是列表,如上所示。这是一个非常有用的东西,因为它意味着你可以在更多的情况下使用它。
In this case, a
is the entire Maybe Tuple
, b
is just the first Just
in the tuple, and c
and d
are the values contained in the first and second Just
in the tuple respectively
在这种情况下,a是整个Maybe Tuple,b只是第一个Just in the tuple,c和d分别包含在元组中的第一个和第二个Just中
#4
To add to what the other people have said, they are called as-patterns (in ML the syntax uses the keyword "as"), and are described in the section of the Haskell Report on patterns.
为了增加其他人所说的内容,它们被称为as-patterns(在ML中,语法使用关键字“as”),并在Haskell模式报告部分中进行了描述。