Quantcast

Help with how to concatenate with own datatypes

classic Classic list List threaded Threaded
3 messages Options
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Help with how to concatenate with own datatypes

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

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!
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Help with how to concatenate with own datatypes

Antoine Latter-2
On Thu, Mar 10, 2011 at 7:41 PM, eldavido <[hidden email]> 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
> [hidden email]
> http://www.haskell.org/mailman/listinfo/haskell-cafe
>

_______________________________________________
Haskell-Cafe mailing list
[hidden email]
http://www.haskell.org/mailman/listinfo/haskell-cafe
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Help with how to concatenate with own datatypes

eldavido
Yeah, that works! Thanks!
Loading...