4 ms·
Or just: fn (Just q) (Just z) = q + z fn _ _ = 0
by zenhack 9y ago
Or just:
fn (Just q) (Just z) = q + z
fn _ _ = 0
- brabel 9y agoWouldn't this let me call `fn true false` or `fn "hi" 2`? Or the second declaration takes the inferred type of the arguments from the first one?
- andolanra 9y agoThis isn't two functions: it's actually one function defined by cases. You can think of it as equivalent to fn x y = case (x, y) of (Just q, Just z) -> q + z (_, _) -> 0 but the case becomes implicit in the repetition of the function name at the top level of indentation.
- dllthomas 9y ago> at the top level of indentation At the same level, really - it works in a let or a where as well.
- masklinn 9y agoSurely since you have monads you can just use the tools they give you? fn = liftM2 (+)
- zenhack 9y agoThat doesn't do quite the same thing; the original example gets rid of the Maybe, defaulting to 0.
- masklinn 9y ago> That doesn't do quite the same thing You're missing the point entirely. > the original example gets rid of the Maybe, defaulting to 0. http://hackage.haskell.org/package/base-4.10.1.0/docs/Data-Maybe.html#v:fromMaybe http://hackage.haskell.org/package/base-4.10.1.0/docs/Data-M...