0

I am reading that Haskell's do notation is quite harmful. Since I am still learning Haskell, I would like to not make bad habits. I am learning a library called Reflex and is an example:

import Reflex.Dom

main = mainWidget $ el "div" $ do
  t <- textInput def
  dynText $ _textInput_value t

I am reading that do notation has to do with the presence of monads such as IO and has to do with <- and $ operators.

How could I write these few lines without do ?


If you support do... could you explain it's use in Haskell and this example?

Community
  • 1
  • 1
john mangual
  • 7,718
  • 13
  • 56
  • 95
  • 5
    do notation is not harmful. in fact, do-notation has been expanded to applicatives, so we're encouraging its use even more. – ErikR Jul 05 '16 at 23:07
  • 1
    Every single answer on the question you linked says that `do` notation is useful and should not be avoided. – interjay Jul 05 '16 at 23:11

1 Answers1

2

do-notation is pure syntactic sugar. It translates into calls to >>= in a pretty simple way. Quoting the Haskell Report:

Translation: Do expressions satisfy these identities, which may be used as a translation into the kernel, after eliminating empty stmts:

do {e}    =   e
do {e;stmts}  =   e >> do {stmts}
do {p <- e; stmts}    =   let ok p = do {stmts}
    ok _ = fail "..."
  in e >>= ok
do {let decls; stmts} =   let decls in do {stmts}

This means your example would look like this:

main = mainWidget $ el "div" $
  textInput def >>= $ \t ->
  dynText $ _textInput_value t

There's nothing harmful about do-notation. I don't know where you got that idea from! It's literally a different way of writing the same code.

Benjamin Hodgson
  • 42,952
  • 15
  • 108
  • 157