Search code examples
haskelldo-notation

Haskell do notation to bind


I´am trying to desugar a do statement in Haskell. I have found some examples here on SO but wasn´t able to apply them to my case. Only thing I can think of is a heavy nested let statement, which seems quite ugly.

Statement in which do notation should be replaced by bind:

do num <- numberNode x
   nt1 <- numberTree t1
   nt2 <- numberTree t2
   return (Node num nt1 nt2)

Any input is highly appreciated =)


Solution

  • numberNode x >>= \num ->
      numberTree t1 >>= \nt1 ->
        numberTree t2 >>= \nt2 ->
          return (Node num nt1 nt2)
    

    Note that this is simpler if you use Applicatives:

    Node <$> numberNode x <*> numberTree t1 <*> numberTree t2