Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to remove the trivial parameter?

Tags:

haskell

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?

like image 354
hliu Avatar asked Dec 06 '25 14:12

hliu


1 Answers

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.

like image 159
luqui Avatar answered Dec 08 '25 17:12

luqui



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!