Hi Alvaro,
as for your second question
2. In that vein, is there an existing function for "a value
or a default if it's zero"? E.g.:
orElse :: (Monoid m) => m -> m -> m
a `orElse` b = if zero a then b else a
There is the function orElse from the syb package [1] that works
on (Maybe a) values. It can be considered a particular instance of
the above with mempty = Nothing.
Alternatively, the function fromMaybe from Data.Maybe [2] provides
a similar functionality, but with the heterogeneous type
fromMaybe :: a -> Maybe a -> a
Essentially, in both cases the zero predicate is specialised to a
pattern matching for Nothing, which doesn't require an Eq
instance. Also, there is no need for a mappend function, which may
be more convenient.
Best regards,
Nikita
[1]
http://hackage.haskell.org/package/syb-0.4.1/docs/Data-Generics-Aliases.html#v:orElse
[2]
http://hackage.haskell.org/package/base-4.6.0.1/docs/Data-Maybe.html#v:fromMaybe
On 29/01/14 07:46, Michael Snoyman wrote: