1

I'm trying to understand Haskell monads and wrote this test program, which compiles and works as expected:

divide :: Int -> Int -> Either String Int
divide _ 0 = Left "Divide by zero error
divide numerator denom = Right (numerator `div` denom)
processNumsMonadically :: Int -> Int -> Either String Int
processNumsMonadically n d = divide n d >>= \q -> return (q+1)

When I try using the word bind instead of the >>= operator in the latter function definition:

processNumsMonadically n d = bind (divide n d) (\q -> return (q+1))

I get the error:

Not in scope: 'bind'

What is the correct way to use the word bind?

asked Mar 29, 2015 at 18:28
3
  • 4
    Note that bind is not a keyword. It's a name people use to refer to >>= operator. You can define bind = (>>=) if that helps. Commented Mar 29, 2015 at 18:37
  • Thank you for correcting me. I should have known it was a name, not a built-in keyword. I'll edit my post (and leave this comment if you leave yours). Commented Mar 29, 2015 at 19:10
  • 2
    Indeed almost nothing is a keyword in Haskell (class, instance, type, newtype, data, case'of, do, if'then'else, let'in, where: those are pretty much all that can occur in the actual code part. Plus a few more which are only used in the module header, and a few from GHC extensions – the full list is here.) All other words are simply names of functions/actions, defined in some library (if not in your own code); you can use Hoogle or Hayoo to find them. Commented Mar 29, 2015 at 20:26

1 Answer 1

6

This isn't a part of Prelude; it resides in Control.Monad.Extra, a part of monad-extras package.


However, you can call operators in prefix manner (like named functions) easily:

processNumsMonadically n d = (>>=) (divide n d) (\q -> return (q+1))

You could also just use do notation:

processNumsMonadically n d = do
 q <- divide n d
 return (q+1)

But while we're at it, I'd write using a Functor:

processNumsMonadically n d = fmap (+1) (divide n d)

or Applicative syntax:

processNumsMonadically n d = (+1) <$> divide n d

You could also lift the +1 to avoid the need for return and the lambda.


As a personal style remark, bind used as a word isn't idiomatic, and IMHO you shouldn't use it.

answered Mar 29, 2015 at 18:31
Sign up to request clarification or add additional context in comments.

3 Comments

Thank you for the answer and advice. FYI, I wasn't planning to use bind as a word in programs, just as an intermediate step when teaching monads. I appreciate the do syntax.
@espertus sorry, can't help it; I just keep seeing the more and more ways to write the function :)
Thanks for the improvements, although I'm just trying to stay one step ahead of my students. :-)

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.