[Haskell-beginners] Drawing Information from a function already defined

Clockwork PC clockworkpc at gmail.com
Thu Jul 21 15:53:37 CEST 2011


Thanks to everyone for such a response! I'm overwhelmed.

Your suggestions were very helpful and I'd like to share my first success in
Haskell with you...

I wrote a little file triangle.hs that contains this:

rightTriangles = [ (a,b,c) | c <- [1..10], b <- [1..10], a <- [1..10], a^2 +

b^2 == c^2 ]

And then loaded it in GHCi:

*GHCi, version 7.0.3: http://www.haskell.org/ghc/  :? for help
Loading package ghc-prim ... linking ... done.
Loading package integer-gmp ... linking ... done.
Loading package base ... linking ... done.
Prelude> :l triangle
[1 of 1] Compiling Main             ( triangle.hs, interpreted )
Ok, modules loaded: Main.
*Main>
*
Then I tried the first approach:

*Main> let myTriangle = [ (a,b,c) | (a,b,c) <- rightTriangles, a+b+c == 24 ]
*Main> myTriangle
[(8,6,10),(6,8,10)]

I also tried the second simple approach:

*Main> let myTriangles2 = filter sum24 rightTriangles where sum24 (a,b,c) =
(a+b+c) == 24
*Main> myTriangles2
[(8,6,10),(6,8,10)]

Hooray!  Thank you, John and Brent.
Thank you also to Felipe for your correction.
And finally, think you to Aditya.  I don't really understand Monads yet, but
when I get to that chapter in my guide I'll refer to your email many times,
I'm sure.

Thanks again to all you lovely people for helping me with my query :-)

Alexander



On Wed, Jul 20, 2011 at 23:57, john melesky <list at phaedrusdeinus.org> wrote:

> On Wed, Jul 20, 2011 at 11:45:52PM +1000, Clockwork PC wrote:
> > Defined my function:
> >
> > Prelude> let rightTriangles = [ (a,b,c) | c <- [1..10], b <- [1..10], a
> <-
> > [1..10], a^2 + b^2 == c^2 ]
> >
> > ...
> >
> > Now, I want to define a refinement of this that will only select values
> > whose total is 24.
> >
> > ...
> >
> > Basically, I'm trying to work out how to draw data from a list already to
> > hand.
>
> This last part says it. You already know how to draw data from a list:
> you do it above when you pull, e.g., c from [1..10]. You actually do
> it three times (for a, b, and c).
>
> This time around, you have an existing list (named rightTriangles),
> and you want to pull (a,b,c) from it, rather than pulling a, b, and c
> from separate lists. Otherwise, it's much the same as what you were
> doing before.
>
> So the structure of it would look something like:
>
>    let myTriangles = [ (a,b,c) | (a,b,c) <- rightTriangles, *condition* ]
>        ^ a new name    ^ same sort of output ^ from your existing list
>
> where *condition* is your "add up to 24" requirement, in the same form
> as the "a^2+b^2==c^2" requirement in your first list comprehension.
>
> Hope that helps.
>
> -john
>
>
> _______________________________________________
> Beginners mailing list
> Beginners at haskell.org
> http://www.haskell.org/mailman/listinfo/beginners
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://www.haskell.org/pipermail/beginners/attachments/20110721/0bff3f9c/attachment.htm>


More information about the Beginners mailing list