How to produce two lists in Haskell

Viewed 1295

How do I take a list in haskell and produce two lists as an output?

For example a question I am working on asks to create a program that takes a predicate and a list and returns a list of two lists, the first those elements of the input list which pass the predicate, the second those that don't, in order, e.g

 tear (>5) [1,10,2,12,3,13]

=> [[10,12,13],[1,2,3]]

What i have so far:

tear f [] = []
tear f (x:xs)
 | f x = x: tear f xs
 | otherwise = tear f xs

Which produces

tear (>5) [1,10,2,12,3,13]
[10,12,13]
1 Answers

Following @WillemVanOnsem's suggestion to use a 2-tuple:

tear f [] = ([],[])
tear f (x:xs)
 | f x = (x:y,z)
 | otherwise = (y,x:z)
    where (y,z) = tear f xs
Related