[Haskell-cafe] Help with how to concatenate with own datatypes

Antoine Latter aslatter at gmail.com
Fri Mar 11 03:58:23 CET 2011


On Thu, Mar 10, 2011 at 7:41 PM, eldavido <eldavido_ at hotmail.com> wrote:
> 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-datatypes-tp3424433p3424433.html
> Sent from the Haskell - Haskell-Cafe mailing list archive at Nabble.com.
>
> _______________________________________________
> Haskell-Cafe mailing list
> Haskell-Cafe at haskell.org
> http://www.haskell.org/mailman/listinfo/haskell-cafe
>



More information about the Haskell-Cafe mailing list