문제

Would it be (mostly) correct to say that the following are the parallels to the Haskell Monad operations in the imperative world?

  • Monad's >> ~ C/C++/JavaScript/etc. , operator
  • do expressions ~ C/C++/JavaScript/etc. ; operator (kinda)
  • MonadFail's fail ~ C/C++/JavaScript/etc. throw

To demonstrate the point, some examples:

Sequentially evaluate, but discard first result:

Haskell:

a = doSomething >> doSomethingElse

C++:

auto a = (doSomething(), doSomethingElse());

JavaScript:

const a = (doSomething(), doSomethingElse());

Evaluate one after the other, reusing previous results:

Haskell:

do a <- doSomething
   doSomethingWith a

JavaScript:

const a = doSomething();
doSomethingWith(a);
도움이 되었습니까?

해결책

If it helps you wrap your head around it, your intuition is a decent analogy for some monads. Consider, however, the list monad:

Prelude> [1, 2, 3] >> [4, 5, 6]
[4,5,6,4,5,6,4,5,6]
Prelude> [1, 2, 3] >>= \x -> map (*x) [4, 5, 6]
[4,5,6,8,10,12,12,15,18]
-- Doesn't even have a fail

Monads are a much more abstract concept than most imperative programmers are accustomed to dealing with. It's okay to start on the path to understanding them by focusing on a few specific monads, but long term don't make your understanding of them too narrow.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 softwareengineering.stackexchange
scroll top