Cory,<br><br>The big hit for me was Phillip Wadler's paper "Monads for functional programming" I made me start thinking "well, this looks like a monad..."<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"><<a href="mailto:nanothief@gmail.com">nanothief@gmail.com</a>></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'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 "what is a monad"); I understand the way to declare something as a monad instance, but I just don'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 < y < z < 100<br>
Find solutions for x,y,z<br>
<br>
The program:<br>
res :: [(Int,Int,Int)]<br>
res = do<br>
x <- [1..100]<br>
y <- [1..100]<br>
z <- [1..100]<br>
guard $ x + 8 * y == 114<br>
guard $ 3*x - 8*y + 4*z == 182<br>
guard $ x < y<br>
guard $ y < z<br>
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's a square number,<br>
(2) it's a cube number, and<br>
(3) the number is under 500000.<br>
<br>
the program<br>
answer = do<br>
d1 <- [0..9]<br>
d2 <- [0..9]<br>
d3 <- [0..9]<br>
d4 <- [0..9]<br>
d5 <- [0..9]<br>
d6 <- [0..9]<br>
let digitSum = d1 + d2 + d3 + d4 + d5 + d6<br>
let value = d1 + d2*10 + d3*100 + d4*1000 + d5*10000 + d6*100000<br>
guard $ digitSum == 43<br>
let lessThan500000 = digitSum < 500000<br>
let isSquare = (round $ sqrt (fromIntegral value)) ^ 2 == value<br>
let isCube = (round $ (fromIntegral value) ** (1/3)) ^ 3 == value<br>
guard $ length (filter id [lessThan500000,isSquare,isCube]) == 2<br>
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>