[Haskell-cafe] Using Product Algebraic types

Crypt Master cryptmaster at hotmail.com
Sun Jul 4 09:59:46 EDT 2004


My question is how do I select or work with product Alhebraic types ? Let 
you give you some context:

I need to be able to work with a list of items whos structure is onyl 
partially know. That is at the level of this module I dont care about what 
rest of it is. So I have this:

type Fitness = Integer
data Population a = Population [Fitness a]

Hopefully this reads Population is constructed using the Population 
constructor and is a list who elements each conists a fitness value and some 
other value. Since I cant do poloymorphioc types with synonyms I went with 
the data type.

My current task is to build a roulette wheel distribution of the fitness 
value. Basically I want to build and incremental summing of the fitness 
value so that each individual is paired with its upper range like so

Population [10 x, 20 y, 30 z]
New Population = [10 x, 20+10 y, 30+30 z]

My first thought was a list comprehension:

rwList :: Population -> Population
rwList (Population popList) = Population [ (rwListSum i)  | i <- popList]

rwListSum :: [a] -> [a]
rwListSum (fitness individual) = (fitness*2, individual)

ingoring for a minute that this wont achieve the above goal, I immediatly 
ran into the issue of how do I represent in the inner portion of the 
Population List, i.e "Fitness a".

"(fitness individual)" doesnt work ? I tried treating it as 2 normal 
arguments, but of course how can I return it then ... ? A function can only 
return one thing. If its not a tuple then it wont work. Hence my question of 
how you represent product types ?

I did come up with one idea, that is of making the internal part a type on 
its own so now I have this:

type Fitness = Integer
data InternalIndividual a = InternalIndividual Fitness a
data Population a = Population [InternalIndividual a]


rwList :: Population -> Population
rwList (Population popList) = Population [ (rwListSum i)  | i <- popList]

rwListSum :: InternalIndividual -> InternalIndividual
rwListSum (InternalIndividual fitness individual) = (InternalIndividual 
fitness*2 individual)

To my surpirse this compiles. I have been able to fully test it yet though.

So is there a way to do this without a extra type ? Is there a better way to 
represent this ?

Thanks for your time,

PS Sorry for all the dumb questions, I always find asking better than just 
wondering for ages.

MSN 8 with e-mail virus protection service: 2 months FREE* 

More information about the Haskell-Cafe mailing list