Use sharing in the Alternative instance of Maybe

Jon Fairbairn jon.fairbairn at cl.cam.ac.uk
Thu Dec 15 11:02:04 CET 2011


Christian Maeder <Christian.Maeder at dfki.de> writes:

> Am 14.12.2011 13:17, schrieb Bas van Dijk:
>> On 14 December 2011 12:51, Michael Snoyman<michael at snoyman.com>  wrote:
>>> Out of curiosity, does this actually bypass an extra allocation, or is
>>> GHC smart enough to notice the possibility to share?
>>
>> With optimizations on, GHC seems to be smart enough. Since the
>> optimized core of:
>>
>> plus1 Nothing  r = r
>> plus1 (Just x) _ = Just x
>
> I suggest to directly write this using case:
>
>  l <|> r = case l of
>    Nothing -> r
>    Just _ -> l

Why not

 plus1 Nothing  r = r
 plus1 l@(Just x) _ = l

or the equivalent written with <|>?

-- 
Jón Fairbairn                                 Jon.Fairbairn at cl.cam.ac.uk





More information about the Libraries mailing list