3 ms·
Just think about it. Let's call such a function fromJust: fromJust :: Maybe a -> a fromJust (Just x) = x fromJust Nothing = ??? -- What can we put
by DanWaterworth 13y ago
Just think about it. Let's call such a function fromJust:
fromJust :: Maybe a -> a
fromJust (Just x) = x
fromJust Nothing = ??? -- What can we put here?
Haskell doesn't have a null value that we could use here. There is such a function defined in the standard library, but it is partial. It uses error in the Nothing case.
edit:
In many cases there is a clear value that should be used in the Nothing case, suppose you have a Maybe Int which is the number of times a specific event happened in the last second. Clearly 0 is a good default and you can write a function (Maybe Int -> Int). There's a function I tend to use for such cases: (maybe :: b -> (a -> b) -> Maybe a -> b). In this case:
maybe 0 id :: Maybe Int -> Int