Personal tools

Simple monad examples

From HaskellWiki

(Difference between revisions)
Jump to: navigation, search
 
Line 7: Line 7:
 
</haskell>
 
</haskell>
   
Which results in <haskell>Just 6</haskell>.
+
Which results in:
  +
  +
<haskell>Just 6</haskell>
   
 
Some simple exercises:
 
Some simple exercises:

Revision as of 02:38, 19 March 2006

This page is designed to show some simple examples of using monads.

I personally found that I reached monad-enlightenment once I contrived this simple example while playing around to see the "guts" of a monadic expression:

Just 5 >>= (\ x -> if (x == 0) then fail "zero" else Just (x + 1) )

Which results in:

Just 6

Some simple exercises:

What would the following snippets resolve to?

Just 0 >>= (\ x -> if (x == 0) then fail "zero" else Just (x + 1) )
Nothing >>= (\ x -> if (x == 0) then fail "zero" else Just (x + 1) )