Separate a Haskell list into an odd and even tuple

1

I have the following function:

ParesImpares:: [Integer] -> ([Integer],[Integer])

I have to draw the odd and even pairs from a list:

ParesImpares [3,2,4,5,7]

- > ([2,4],[3,5,7])

My code so far is this:

    paresImpares :: [Integer] -> ([Integer],[Integer])
    paresImpares [] = ([],[])
    paresImpares [x] 
    | x 'mod' 2 == 0 = ([x],[])
    | otherwise = ([],[x]) 

I have a base case defined, but the truth is that I do not know how to continue. I had something like:

ParesImpares(x:xs) = ParesImpares [x]

But of course, only the first item on the list evaluates me.

    
asked by Ramosaurio 13.10.2016 в 11:26
source

2 answers

1

There is a more direct solution:

import Data.List (partition) 

paresImpares :: Integral a => [a] -> ([a],[a])
paresImpares = partition even
    
answered by 14.10.2016 / 01:42
source
0

Well I found an answer:

paresImpares :: Integral a => [a] -> ([a],[a])
paresImpares [] = ([],[])
paresImpares (x:xs)
| x 'mod' 2 == 0 = (x:pares,impares)
| otherwise = (pares, x:impares)
where (pares,impares) = paresImpares xs

This works perfectly x mod 2 can be replaced by even

    
answered by 13.10.2016 в 13:04