Old exam question

46 views
Skip to first unread message

Henrik Larsson

unread,
Jan 13, 2015, 4:22:01 AM1/13/15
to chalmer...@googlegroups.com
Question 2c of 2013-12-17 uses Just [b,c,d] instead of return [b,c,d]. Is it not better to use the more generic version of return instead of Just?

Jeff chen

unread,
Jan 13, 2015, 4:51:59 AM1/13/15
to chalmer...@googlegroups.com

I guess it depends on whether you'd like to see Maybe as a data type or a monad,
'Just' is constructor whereas 'return' is a function defined in Monad typeclass. 
and 'Maybe' is an instance of Monad, with definition of 'return' as 'Just'.

data Maybe a = Nothing | Just a
 
instance Monad Maybe where
    return         = Just
    fail           = Nothing
    Nothing  >>= f = Nothing
    (Just x) >>= f = f x

So it's not more generic in this particular case, but I do agree on using return instead of constructor directly will provide more monadic style. 

Henrik Larsson於 2015年1月13日星期二 UTC+1上午10時22分01秒寫道:
Reply all
Reply to author
Forward
0 new messages