Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the meaning of the '@' operator in Haskell

I have this code:

  mix xxs@(x:xs) yys@(y:ys)
  | x<=y = x : mix xs yys
  | otherwise = y : mix xxs ys

But I don't know what the @ means.

like image 360
050 Avatar asked Jan 26 '26 00:01

050


2 Answers

It's not an operator; it's part of the pattern syntax. In the case of yys@(y:ys), if the second argument successfully matches against (y:ys), the entire value matched is bound to yys as well.

So, if you called min [1,2] [3,4], then y would be bound to 3, ys to [4], and yys to [3,4].

like image 167
chepner Avatar answered Jan 28 '26 19:01

chepner


This is an as-pattern [Haskell tutorial] it is used to have a reference both to the entire list xxs, the head of the list x and the tail of the list xs (and of course the same for the second list).

The pattern will only fire if all the subpatterns match as well, so in this case if both the two lista re non-empty. It is often used as an optimization, since now you can use the first or second parameter xxs or yys in the body of the function.

like image 32
Willem Van Onsem Avatar answered Jan 28 '26 20:01

Willem Van Onsem



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!