Cory,<br><br>The big hit for me was Phillip Wadler&#39;s paper &quot;Monads for functional programming&quot; I made me&nbsp;start thinking &quot;well, this looks like a monad...&quot;<br><br><a href="http://homepages.inf.ed.ac.uk/wadler/papers/marktoberdorf/baastad.pdf">homepages.inf.ed.ac.uk/wadler/papers/marktoberdorf/baastad.pdf</a><br>
<br>Cheers<cite><br></cite><br><div class="gmail_quote">On Thu, Jan 29, 2009 at 01:38, nanothief <span dir="ltr">&lt;<a href="mailto:nanothief@gmail.com">nanothief@gmail.com</a>&gt;</span> wrote:<br><blockquote class="gmail_quote" style="border-left: 1px solid rgb(204, 204, 204); margin: 0pt 0pt 0pt 0.8ex; padding-left: 1ex;">
<div><div></div><div class="Wj3C7c">Cory Knapp wrote:<br>
<blockquote class="gmail_quote" style="border-left: 1px solid rgb(204, 204, 204); margin: 0pt 0pt 0pt 0.8ex; padding-left: 1ex;">
Hello, so, I&#39;m having a simple problem with monads: I have no idea how to actually use them. I understand the category theory (or, at least well enough to be able to explain &quot;what is a monad&quot;); I understand the way to declare something as a monad instance, but I just don&#39;t get how to program with them. Can anyone provide me with, or direct me towards, some simple monads and some ways of using (for example) the monadic properties of lists?<br>

<br>
Thanks,<br>
Cory<br>
_______________________________________________<br>
Beginners mailing list<br>
<a href="mailto:Beginners@haskell.org" target="_blank">Beginners@haskell.org</a><br>
<a href="http://www.haskell.org/mailman/listinfo/beginners" target="_blank">http://www.haskell.org/mailman/listinfo/beginners</a><br>
</blockquote></div></div>
I found that <a href="http://www.haskell.org/all_about_monads/html/index.html" target="_blank">http://www.haskell.org/all_about_monads/html/index.html</a> gave a lot of nice examples of using list and maybe monads. The list monad is particularly useful for finding possible solutions given available input values.<br>

For example, with the problem<br>
x + 8y = 114<br>
3x - 8y + 4z = 182<br>
x &lt; y &lt; z &lt; 100<br>
Find solutions for x,y,z<br>
<br>
The program:<br>
res :: [(Int,Int,Int)]<br>
res = do<br>
&nbsp;x &lt;- [1..100]<br>
&nbsp;y &lt;- [1..100]<br>
&nbsp;z &lt;- [1..100]<br>
&nbsp;guard $ x + 8 * y == 114<br>
&nbsp;guard $ 3*x - 8*y + 4*z == 182<br>
&nbsp;guard $ x &lt; y<br>
&nbsp;guard $ y &lt; z<br>
&nbsp;return (x,y,z)<br>
<br>
will output all the possible solutions. Note how close the program is to the actual problem. The values of x,y, and z are chosen from the value [1..100], but if a guard statement fails, the (x,y,z) choice is abandoned.<br>

<br>
Another example (taken from <a href="http://www.mathsisfun.com/puzzles/sum-of-digits-is-43-solution.html" target="_blank">http://www.mathsisfun.com/puzzles/sum-of-digits-is-43-solution.html</a> )<br>
*The Puzzle:* I am thinking of a 6-digit number. The sum of the digits is 43.<br>
<br>
And only two of the following three statements about the number are true:<br>
<br>
(1) it&#39;s a square number,<br>
(2) it&#39;s a cube number, and<br>
(3) the number is under 500000.<br>
<br>
the program<br>
answer = do<br>
&nbsp;d1 &lt;- [0..9]<br>
&nbsp;d2 &lt;- [0..9]<br>
&nbsp;d3 &lt;- [0..9]<br>
&nbsp;d4 &lt;- [0..9]<br>
&nbsp;d5 &lt;- [0..9]<br>
&nbsp;d6 &lt;- [0..9]<br>
&nbsp;let digitSum = d1 + d2 + d3 + d4 + d5 + d6<br>
&nbsp;let value = d1 + d2*10 + d3*100 + d4*1000 + d5*10000 + d6*100000<br>
&nbsp;guard $ digitSum == 43<br>
&nbsp;let lessThan500000 = digitSum &lt; 500000<br>
&nbsp;let isSquare = (round $ sqrt (fromIntegral value)) ^ 2 == value<br>
&nbsp;let isCube = (round $ (fromIntegral value) ** (1/3)) ^ 3 == value<br>
&nbsp;guard $ length (filter id [lessThan500000,isSquare,isCube]) == 2<br>
&nbsp;return value<br>
<br>
will output the three answers (not that the author only found one solution!).<div><div></div><div class="Wj3C7c"><br>
<br>
<br>
<br>
_______________________________________________<br>
Beginners mailing list<br>
<a href="mailto:Beginners@haskell.org" target="_blank">Beginners@haskell.org</a><br>
<a href="http://www.haskell.org/mailman/listinfo/beginners" target="_blank">http://www.haskell.org/mailman/listinfo/beginners</a><br>
</div></div></blockquote></div><br><br clear="all"><br>-- <br>Rafael Gustavo da Cunha Pereira Pinto<br>Electronic Engineer, MSc.<br>