1
$\begingroup$

Do-notation is most famous by Haskell, which is also famous for having lazy evaluation.

But is lazy evaluation a requirement for implementing do-notation?

In principle, could do-notation be added to imperative languages, such as Java and C++?

$\endgroup$
  • 1
    $\begingroup$ For the benefit of people who don't know Haskell, what is "do-notation"? It feels like a Java/C/whatever expert who doesn't know Haskell might be able to answer your question, if you explain what "do-notation" is. $\endgroup$ – David Richerby Mar 4 at 15:35
  • $\begingroup$ @DavidRicherby I added a link, but I think to answer this question it would be best to already know Haskell (or FP) and Java / C++. $\endgroup$ – sdgfsdh Mar 4 at 15:57
  • 1
    $\begingroup$ OK -- that sounds reasonable. Thanks! $\endgroup$ – David Richerby Mar 4 at 15:58
  • $\begingroup$ C#'s LINQ syntax and Scala's for-comprehensions are actual examples of do-notation equivalents in mainstream eager, imperative languages. $\endgroup$ – Derek Elkins Mar 5 at 8:17
2
$\begingroup$

It is not required that the language is lazy. The do notation exists in SML family of languages (for instance OCaml), which are eager. In these languages the do-notation is written as the let-notation, that is, instead of writing

do x1 <- e1
   x2 <- e2
   ...
   return e

we write

let x1 = e1 in
let x2 = e2 in
...
e

Note however that let in OCaml has a fixed monad (what in Haskell would be the IO monad), whereas the do notation in Haskell works for any monad. There are extensions of OCaml which allow you to use any monad.

$\endgroup$
1
$\begingroup$

Sure, why not? F# is eager (not lazy) and has a sort-of do-notation.

Imperative PLs might add that as well, even if there's less need for monads there, since side effects (including mutating the values of variables) is a key aspect of those languages.

Scala has a for loop which resembles a monadic comprehension, so it's quite close to a do-block.

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.