1

I have the following function

bar :: (Monad m, Num b) => m b -> m b -> m b -> m b 
bar x y z = do
              x' <- x
              y' <- y
              z' <- z
              return $ x' + y' + z'

and I'm trying to write it without the 'do' block. I tried

bar' :: (Monad m, Num b) => m b -> m b -> m b -> m b
bar' x y z = x >>=  ( \x' -> y >>= (\y' -> z >>= (\z' -> (x' + y' + z') ) ))

But I can't get it to work - I get

  error:
    * Occurs check: cannot construct the infinite type: b ~ m b
    * In the expression: x' + y' + z'

Any idea ?

Thanks!

Will Ness
  • 70,110
  • 9
  • 98
  • 181
Tomer
  • 1,159
  • 7
  • 15
  • 7
    You forgot the return `x >>= ( \x' -> y >>= (\y' -> z >>= (\z' -> return (x' + y' + z') ) ))` – Garrison Mar 11 '20 at 03:18
  • Good point ... thanks!! – Tomer Mar 11 '20 at 03:20
  • 2
    Who is voting to close this as "Cannot reproduce, or simple typo"? This is obviously very easy to reproduce, and forgetting `return` is a clear semantic issue, not a slip of the finger. – amalloy Mar 11 '20 at 04:31
  • 1
    @GilShafriri it is perfectly OK to self-answer your question if you've realized what the error was and how to fix it now. – Will Ness Mar 11 '20 at 10:18
  • 1
    I'm not sure why the downvotes? "Here's my problem" - Check. "Here's what I tried" - check. "Here's what happened" - check. –  Mar 11 '20 at 10:18
  • 1
    @amalloy "can *no longer* reproduce". If it's a very common mistake that is easily pointed out in a comment (as Garrison did), I'm comfortable defending the close vote. – chepner Mar 11 '20 at 11:23
  • 1
    @amalloy If it makes the asker feel "oh *duh*, silly me!" then it's a simple typo. If the asker needs an explanation about what `return` does and why you need it, then it's not a simple typo. – user253751 Mar 11 '20 at 15:20

0 Answers0