I'm new to functional programming, so working through 'Type Driven Development with Idris'. Give a function maxMaybe that can compute the max between two Maybe Doubles, defines as below
maxMaybe : Ord a => Maybe a -> Maybe a -> Maybe a
maxMaybe Nothing y = y
maxMaybe x Nothing = x
maxMaybe Nothing Nothing = Nothing
maxMaybe (Just x) (Just y) = Just (max x y)
Is there a way to collapse the following cases into just one, since the positions don't matter?
maxMaybe Nothing y = y
maxMaybe x Nothing = x
I would also be curious to know if this kind of thing is handled generally in other purely functional programming languages like Haskell or OCaml
maxMaybe : Ord a => Maybe a -> Maybe a -> Maybe a
maxMaybe Nothing y = y
-- maxMaybe x Nothing = x
maxMaybe Nothing Nothing = Nothing
maxMaybe (Just x) (Just y) = Just (max x y)
maxMaybe (Just 5) (Nothing) maxMaybe (Just 5) Nothing : Maybe Integer
You can write
but it will probably be more expensive at runtime as you'll redo a lot of the case analysis unless enough compiler optimisations kick in to give you something essentially equivalent to your original solution.