separate even and odd elements of list haskell

  • A+
Category:Languages

I am trying to separate elements of a list into to further lists, one for the odd and one for even numbers.

For Example,

input: [1,2,3,4,10] output: ([2,4,10],[1,3])  sepList :: [Int]->([Int],[Int]) sepList [] = ([],[]) sepList (x:xs) | (x mod 2) ==0 = (([x],[]) ++ sepList xs)                 | (x mod 2) /=0 = (([],[x]) ++ sepList xs)                | otherwise = ([],[]) 

It gives error on ...++ sepList xs anyone could guide me here?

 


The operator++ is used to concatenate 2 lists and neither of your arguments to ++ is a list,

([x],[]) ++ sepList xs 

both ([x],[]) and sepList xs are pairs of lists. So what you want is to pattern match on sepList xs e.g. using a let binding,

let (ys,zs) = sepList xs in 

and then return,

(x:ys,zs) 

Comment

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen: