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

john melesky list at phaedrusdeinus.org
Wed Jul 20 15:57:39 CEST 2011


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




More information about the Beginners mailing list