What does the "@" symbol mean in reference to lists in Haskell?
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 :: [a] -> [[a]]
tails [] = [[]]
tails xxs@(_:xs) = xxs : tails xs
Yes, it's just syntactic sugar, with @
read aloud as "as". ps@(p:pt)
gives you names for
- the list:
ps
- the list's head :
p
- the list's tail:
pt
Without the @
, you'd have to choose between (1) or (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.