
Hi, I´m doing a project in haskell and I need to define an operator that concatenate some own defined data types, just like the operator ++ does for lists. I don´t see how to define the operator recursively since this adding function (:) doesn´t work on my own data types. This is my code: data Allele = Aone | Atwo deriving Show type Lifespan = Int data Population = Pop [(Allele,Allele,Lifespan)] deriving Show genepool :: Population genepool = Pop [] --can only concatenate 2 elements (+-) :: Population -> Population -> Population Pop [(a,b,c)] +- Pop [] = Pop [(a,b,c)] Pop [(a,b,c)] +- Pop [(d,e,f)] = Pop [(a,b,c),(d,e,f)] --and thats why this function goes non-exhaustive. gpinsertaoneaone :: Int -> Int -> Population gpinsertaoneaone 0 age = genepool gpinsertaoneaone n age = Pop [(Aone,Aone,70-age)] +- gpinsertaoneaone (n-1) age Thx for help and advice! -- View this message in context: http://haskell.1045720.n5.nabble.com/Help-with-how-to-concatenate-with-own-d... Sent from the Haskell - Haskell-Cafe mailing list archive at Nabble.com.

On Thu, Mar 10, 2011 at 7:41 PM, eldavido
Hi, I´m doing a project in haskell and I need to define an operator that concatenate some own defined data types, just like the operator ++ does for lists. I don´t see how to define the operator recursively since this adding function (:) doesn´t work on my own data types. This is my code: data Allele = Aone | Atwo deriving Show type Lifespan = Int data Population = Pop [(Allele,Allele,Lifespan)] deriving Show
I don't know what precise behavior you want to model, but can you use the '++' operator in your definition?
(Pop x) +- (Pop y) = Pop (x ++ y)
Antoine
genepool :: Population genepool = Pop []
--can only concatenate 2 elements (+-) :: Population -> Population -> Population Pop [(a,b,c)] +- Pop [] = Pop [(a,b,c)] Pop [(a,b,c)] +- Pop [(d,e,f)] = Pop [(a,b,c),(d,e,f)]
--and thats why this function goes non-exhaustive. gpinsertaoneaone :: Int -> Int -> Population gpinsertaoneaone 0 age = genepool gpinsertaoneaone n age = Pop [(Aone,Aone,70-age)] +- gpinsertaoneaone (n-1) age
Thx for help and advice!
-- View this message in context: http://haskell.1045720.n5.nabble.com/Help-with-how-to-concatenate-with-own-d... Sent from the Haskell - Haskell-Cafe mailing list archive at Nabble.com.
_______________________________________________ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe

Yeah, that works! Thanks! -- View this message in context: http://haskell.1045720.n5.nabble.com/Help-with-how-to-concatenate-with-own-d... Sent from the Haskell - Haskell-Cafe mailing list archive at Nabble.com.
participants (2)
-
Antoine Latter
-
eldavido