[Haskell-cafe] Re: what does @ mean?.....

Ben Franksen ben.franksen at online.de
Fri Dec 28 10:31:47 EST 2007


Achim Schneider wrote:

> ChrisK <haskell at list.mightyreason.com> wrote:
> 
>> zeroNothing Nothing = Nothing
>> zeroNothing (Just n) =
>>   if n == 0 then Nothing else (Just n)
>> 
>> versus
>> 
>> zeroNothing Nothing = Nothing
>> zeroNothing x@(Just n) =
>>   if n == 0 then Nothing else x
>> 
> versus
> 
> zeroNothing Nothing = Nothing
> zeroNothing x =
>     let (Just n) = x
>     in if n == 0 then Nothing else x
> 
> so, @ is kind of like a let, just with its arguments flipped.

However, if  x@(Just n)  fails to match, the next clause is chosen, whereas
the variable pattern  x  matches always. Thus, the last version works only
because the other possible case (Nothing) has already been handled. IOW, in
the second version of zeroNothing you may swap the order of patterns, but
not in the third one.

Cheers
Ben



More information about the Haskell-Cafe mailing list