What does "@" mean in Haskell?

I tried to search, but do not understand. I am promoting my knowledge of Haskell by reading some articles, and I came across one that uses syntax that I have never seen before. An example is:

reconstruct node@ (Node abclr) parent@ (Node bd le ri) 

I have never seen these @ before. I tried searching the Internet for an answer, but did not understand. Is this just a way to embed labels to make things clearer, or do they actually affect the code?

+7
haskell
source share
1 answer

Used when matching with a pattern. Now the node variable will refer to the entire node data type for the Node abclr argument. So instead of moving to a function like Node abclr you can use node instead to pass it.

A simpler example to demonstrate:

 data SomeType = Leaf Int Int Int | Nil deriving Show someFunction :: SomeType -> SomeType someFunction leaf@ (Leaf _ _ _) = leaf someFunction Nil = Leaf 0 0 0 

someFunction can also be written as:

 someFunction :: SomeType -> SomeType someFunction (Leaf xyz) = Leaf xyz someFunction Nil = Leaf 0 0 0 

See how much easier the first version was?

+13
source share

All Articles