5 ms·
One thing that bothers me, but I found no explanation why - is there a way to turn 'Just a' to 'a'? >>= seems like it does that but I only see it working with
by Ygg2 14y ago
One thing that bothers me, but I found no explanation why - is there a way to turn 'Just a' to 'a'? >>= seems like it does that but I only see it working with function that returns a Monad.
EDIT: I may have skimmed a bit.
- DanWaterworth 14y agoJust 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
- Peaker 14y agoMaybe a is a value that may or may not contain an a within it. So how would you convert that to an a, which definitely has an a in it? What you can do, is pass a callback to a function that handles the case where you do have an "a" in the Maybe. And that's basically what fmap, <$>, and >>= do.
- yummyfajitas 14y agoimport Data.Maybe let x = fromMaybe 1 (Just 2) -- x == 2 let y = fromMaybe 1 Nothing -- y = 1
- deleted 14y ago[deleted]