I have a function defined as follow:
errorWhenNothing :: Maybe a -> M (Maybe a) -- M is a Monad
errorWhenNothing Nothing = throwError "is Nothing!"
errorWhenNothing m = return m
The parameter m seems trivial and removing it makes the function simpler and more compact. The problem is that I cannot rewrite the second definition as
errorWhenNothing = return
GHC complains Equations for 'errorWhenNothing' have different numbers of arguments..
I want to know is there a way to remove the m?
Yeah you can't have different numbers of arguments for different clauses of the same function. It's mostly to catch the common error of leaving out a parameter on one of the lines, so instead of a confusing type error it just tells you what you did wrong straight away.
If you don't want the m there, you could make the wholething pointfree with the maybe eliminator
errorWhenNothing :: Maybe a -> M a
errorWhenNothing = maybe (throwError "isNothing!") return
whether this is an improvement is a matter of opinion.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With