I’ve come across a piece of Haskell code that looks like this:
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.
Yes, it’s just syntactic sugar, with
@read aloud as “as”.ps@(p:pt)gives you names forpspptWithout 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], thent@(Tree _ kids)gives you access to both the tree and its children.