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? Thanks, Cory |
Cory Knapp wrote:
> 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 come from the complete other end of the spectrum, someone who does not know category theory, but know many programming languages, including Ocaml function language much like Haskell, but less pure. It is possible to use monads in Ocaml, but most code doesn't. > I understand the > way to declare something as a monad instance, but I just don't get how > to program with them. When it comes to coding with Haskell, you can basically ignore monads and just follow the type signatures. > Can anyone provide me with, or direct me towards, > some simple monads and some ways of using (for example) the monadic > properties of lists? Writing my own monad is something I will tackle when I know I need to. Erik -- ----------------------------------------------------------------- Erik de Castro Lopo ----------------------------------------------------------------- "C++ is like jamming a helicopter inside a Miata and expecting some sort of improvement." -- Drew Olbrich |
In reply to this post by Cory Knapp
Cory Knapp wrote:
> 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? > > Thanks, > Cory > _______________________________________________ > Beginners mailing list > [hidden email] > http://www.haskell.org/mailman/listinfo/beginners 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. For example, with the problem x + 8y = 114 3x - 8y + 4z = 182 x < y < z < 100 Find solutions for x,y,z The program: res :: [(Int,Int,Int)] res = do x <- [1..100] y <- [1..100] z <- [1..100] guard $ x + 8 * y == 114 guard $ 3*x - 8*y + 4*z == 182 guard $ x < y guard $ y < z return (x,y,z) 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. Another example (taken from http://www.mathsisfun.com/puzzles/sum-of-digits-is-43-solution.html ) *The Puzzle:* I am thinking of a 6-digit number. The sum of the digits is 43. And only two of the following three statements about the number are true: (1) it's a square number, (2) it's a cube number, and (3) the number is under 500000. the program answer = do d1 <- [0..9] d2 <- [0..9] d3 <- [0..9] d4 <- [0..9] d5 <- [0..9] d6 <- [0..9] let digitSum = d1 + d2 + d3 + d4 + d5 + d6 let value = d1 + d2*10 + d3*100 + d4*1000 + d5*10000 + d6*100000 guard $ digitSum == 43 let lessThan500000 = digitSum < 500000 let isSquare = (round $ sqrt (fromIntegral value)) ^ 2 == value let isCube = (round $ (fromIntegral value) ** (1/3)) ^ 3 == value guard $ length (filter id [lessThan500000,isSquare,isCube]) == 2 return value will output the three answers (not that the author only found one solution!). |
Cory,
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..." homepages.inf.ed.ac.uk/wadler/papers/marktoberdorf/baastad.pdf Cheers On Thu, Jan 29, 2009 at 01:38, nanothief <[hidden email]> wrote: > Cory Knapp wrote: > >> 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? >> >> Thanks, >> Cory >> _______________________________________________ >> Beginners mailing list >> [hidden email] >> http://www.haskell.org/mailman/listinfo/beginners >> > I found that http://www.haskell.org/all_about_monads/html/index.html 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. > For example, with the problem > x + 8y = 114 > 3x - 8y + 4z = 182 > x < y < z < 100 > Find solutions for x,y,z > > The program: > res :: [(Int,Int,Int)] > res = do > x <- [1..100] > y <- [1..100] > z <- [1..100] > guard $ x + 8 * y == 114 > guard $ 3*x - 8*y + 4*z == 182 > guard $ x < y > guard $ y < z > return (x,y,z) > > 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. > > Another example (taken from > http://www.mathsisfun.com/puzzles/sum-of-digits-is-43-solution.html ) > *The Puzzle:* I am thinking of a 6-digit number. The sum of the digits is > 43. > > And only two of the following three statements about the number are true: > > (1) it's a square number, > (2) it's a cube number, and > (3) the number is under 500000. > > the program > answer = do > d1 <- [0..9] > d2 <- [0..9] > d3 <- [0..9] > d4 <- [0..9] > d5 <- [0..9] > d6 <- [0..9] > let digitSum = d1 + d2 + d3 + d4 + d5 + d6 > let value = d1 + d2*10 + d3*100 + d4*1000 + d5*10000 + d6*100000 > guard $ digitSum == 43 > let lessThan500000 = digitSum < 500000 > let isSquare = (round $ sqrt (fromIntegral value)) ^ 2 == value > let isCube = (round $ (fromIntegral value) ** (1/3)) ^ 3 == value > guard $ length (filter id [lessThan500000,isSquare,isCube]) == 2 > return value > > will output the three answers (not that the author only found one > solution!). > > > > > _______________________________________________ > Beginners mailing list > [hidden email] > http://www.haskell.org/mailman/listinfo/beginners > -- Rafael Gustavo da Cunha Pereira Pinto Electronic Engineer, MSc. -------------- next part -------------- An HTML attachment was scrubbed... URL: http://www.haskell.org/pipermail/beginners/attachments/20090129/5f9caad5/attachment-0001.htm |
Thanks to both of you, I'll look into those.
Cory Rafael Gustavo da Cunha Pereira Pinto wrote: > Cory, > > 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..." > > homepages.inf.ed.ac.uk/wadler/papers/marktoberdorf/baastad.pdf > <http://homepages.inf.ed.ac.uk/wadler/papers/marktoberdorf/baastad.pdf> > > Cheers > > On Thu, Jan 29, 2009 at 01:38, nanothief <[hidden email] > <mailto:[hidden email]>> wrote: > > Cory Knapp wrote: > > 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? > > Thanks, > Cory > _______________________________________________ > Beginners mailing list > [hidden email] <mailto:[hidden email]> > http://www.haskell.org/mailman/listinfo/beginners > > I found that > http://www.haskell.org/all_about_monads/html/index.html 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. > For example, with the problem > x + 8y = 114 > 3x - 8y + 4z = 182 > x < y < z < 100 > Find solutions for x,y,z > > The program: > res :: [(Int,Int,Int)] > res = do > x <- [1..100] > y <- [1..100] > z <- [1..100] > guard $ x + 8 * y == 114 > guard $ 3*x - 8*y + 4*z == 182 > guard $ x < y > guard $ y < z > return (x,y,z) > > 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. > > Another example (taken from > http://www.mathsisfun.com/puzzles/sum-of-digits-is-43-solution.html ) > *The Puzzle:* I am thinking of a 6-digit number. The sum of the > digits is 43. > > And only two of the following three statements about the number > are true: > > (1) it's a square number, > (2) it's a cube number, and > (3) the number is under 500000. > > the program > answer = do > d1 <- [0..9] > d2 <- [0..9] > d3 <- [0..9] > d4 <- [0..9] > d5 <- [0..9] > d6 <- [0..9] > let digitSum = d1 + d2 + d3 + d4 + d5 + d6 > let value = d1 + d2*10 + d3*100 + d4*1000 + d5*10000 + d6*100000 > guard $ digitSum == 43 > let lessThan500000 = digitSum < 500000 > let isSquare = (round $ sqrt (fromIntegral value)) ^ 2 == value > let isCube = (round $ (fromIntegral value) ** (1/3)) ^ 3 == value > guard $ length (filter id [lessThan500000,isSquare,isCube]) == 2 > return value > > will output the three answers (not that the author only found one > solution!). > > > > > _______________________________________________ > Beginners mailing list > [hidden email] <mailto:[hidden email]> > http://www.haskell.org/mailman/listinfo/beginners > > > > > -- > Rafael Gustavo da Cunha Pereira Pinto > Electronic Engineer, MSc. |
In reply to this post by Cory Knapp
I have written a reference manual for the basic Haskell monad functions, "A Tour of the Haskell Monad functions". It contains a lot of examples. You can find it at: http://members.chello.nl/hjgtuyl/tourdemonad.html Regards, Henk-Jan van Tuyl -- http://functor.bamikanarie.com http://Van.Tuyl.eu/ -- On Thu, 29 Jan 2009 03:44:30 +0100, Cory Knapp <[hidden email]> wrote: > 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? > > Thanks, > Cory > _______________________________________________ > Beginners mailing list > [hidden email] > http://www.haskell.org/mailman/listinfo/beginners > -- |
Free forum by Nabble | Edit this page |