3 ms·
Which is awfully similar to Haskell's Maybe. Ignoring boilerplate and comments, the entire module is just [0]: data Maybe a = Nothing | Just a deriving (
by gizmo686 8y ago
Which is awfully similar to Haskell's Maybe. Ignoring boilerplate and comments, the entire module is just [0]:
data Maybe a = Nothing | Just a
deriving ( Eq, Ord )
And the deriving line isn't really nessasary.
The Monad module is larger and contains code specific to Maybe [1]:
instance Monad Maybe where
(Just x) >>= k = k x
Nothing >>= _ = Nothing
(>>) = (*>)
fail _ = Nothing
(Only the first 2 lines are needed. The 3rd is an optimization, the 4th is a historical wart that will be removed in future versions (moved to MonadFail)
[0] https://hackage.haskell.org/package/base-4.12.0.0/docs/src/GHC.Maybe.html#Maybe https://hackage.haskell.org/package/base-4.12.0.0/docs/src/G...
[1] https://hackage.haskell.org/package/base-4.12.0.0/docs/src/GHC.Base.html#line-854 https://hackage.haskell.org/package/base-4.12.0.0/docs/src/G...
- throwaway487549 8y agoAnd this is meaningful only in the context of a lazy language. In the context of strict, statically typed language an algebraic Option type is good-enough.
- gizmo686 8y agoMaybe is an algebraic Option type. It just happens to implement an interface that abstracts away the if(None) checks that you would be doing anyway. Lazyness has nothing to do with it.