
Hello, the "Advanced Monads" page in the Haskell Wikibook (http://en.wikibooks.org/wiki/Haskell/Advanced_monads) contains the following example of a List Monad pythags = do x <- [1..] y <- [x..] z <- [y..] guard (x^2 + y^2 == z^2) return (x, y, z) However, whenever you load that function definition into Hugs or GHCi, you get a message saying that "guard" is an undefined variable. Does anyone know why? Thanks. phiroc

Check out Hoogle: http://haskell.org/hoogle/?q=guard
import Control.Monad
-Greg
On 2/12/07, phiroc@free.fr
Hello,
the "Advanced Monads" page in the Haskell Wikibook (http://en.wikibooks.org/wiki/Haskell/Advanced_monads) contains the following example of a List Monad
pythags = do x <- [1..] y <- [x..] z <- [y..] guard (x^2 + y^2 == z^2) return (x, y, z)
However, whenever you load that function definition into Hugs or GHCi, you get a message saying that "guard" is an undefined variable.
Does anyone know why?
Thanks.
phiroc _______________________________________________ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe

On 2/12/07, phiroc@free.fr
Hello,
the "Advanced Monads" page in the Haskell Wikibook (http://en.wikibooks.org/wiki/Haskell/Advanced_monads) contains the following example of a List Monad
pythags = do x <- [1..] y <- [x..] z <- [y..] guard (x^2 + y^2 == z^2) return (x, y, z)
However, whenever you load that function definition into Hugs or GHCi, you get a message saying that "guard" is an undefined variable.
Does anyone know why?
Thanks.
phiroc
In the context of the tutorial, guard isn't defined until the next section: additive monads.

On Feb 12, 2007, at 11:02 AM, phiroc@free.fr wrote:
Hello,
the "Advanced Monads" page in the Haskell Wikibook (http://en.wikibooks.org/wiki/Haskell/Advanced_monads) contains the following example of a List Monad
pythags = do x <- [1..] y <- [x..] z <- [y..] guard (x^2 + y^2 == z^2) return (x, y, z)
However, whenever you load that function definition into Hugs or GHCi, you get a message saying that "guard" is an undefined variable.
Does anyone know why?
Thanks.
phiroc
Add the line import Control.Monad to the beginning of your program. The 'guard' function is not automatically in scope. Rob Dockins Speak softly and drive a Sherman tank. Laugh hard; it's a long way to the bank. -- TMBG

On Feb 12, 2007, at 11:02 AM, phiroc@free.fr wrote:
Hello,
the "Advanced Monads" page in the Haskell Wikibook (http://en.wikibooks.org/wiki/Haskell/Advanced_monads) contains the following example of a List Monad
pythags = do x <- [1..] y <- [x..] z <- [y..] guard (x^2 + y^2 == z^2) return (x, y, z)
However, whenever you load that function definition into Hugs or GHCi, you get a message saying that "guard" is an undefined variable.
Does anyone know why?
Thanks.
phiroc
Another note about this function -- it doesn't actually work. It will forever try increasing values of z, trying to find a z such that z^2 = 1^2 + 1^2, and no such z exists. The following function, however, does seem to correctly generate all the Pythagorean triples. pythags = do z <- [1..] x <- [1..z] y <- [x..z] guard (x^2 + y^2 == z^2) return (x,y,z) Rob Dockins Speak softly and drive a Sherman tank. Laugh hard; it's a long way to the bank. -- TMBG
participants (4)
-
Creighton Hogg
-
Greg Fitzgerald
-
phiroc@free.fr
-
Robert Dockins