28 Dec 16:31
Re: what does @ mean?.....
Ben Franksen <ben.franksen <at> online.de>
2007-12-28 15:31:47 GMT
2007-12-28 15:31:47 GMT
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
RSS Feed