rmsorPth
rmsorPth

Reputation: 111

how to split a list into two odd and even lists in haskell using splitWhen function and predicate function

I am new to Haskell and I wanted to split a list based on the condition that if elements of list are even or odd using splitWhen function which takes first parameter as boolean condition. This boolean condition is the return of another function called checkEven. But how can I pass the element to checkEven function. Is this even possible ?

checkEven n=(n mod 2==0)
splitList x= splitWhen (checkEven(<???>)) x
main =do 
 print (splitList [0,1,2,3,-1])

Please suggest other ways to solve this problem if above method is wrong. but I was given the same problem.

Thanks in advance

Upvotes: 0

Views: 1620

Answers (2)

rmsorPth
rmsorPth

Reputation: 111

Thanks @AndrewC for answer. I was confused how to split a list and tried to use built in splitWhen function for that. But actually I found solution that do not use splitWhen function but uses prelude function.

splitOE::[Integer]->(Integer->Bool)->([Integer],[Integer])
nums=[1,3,5,2,6,7,12,14,9]
splitOE xs preFunc=(a,b)
        where a=[x|x<-xs,preFunc x]
              b=[x|x<-xs,(not.preFunc) x]
main = do 
        print (splitOE nums even)
*Main> main
([2,6,12,14],[1,3,5,7,9])

This solved my problem although it was not as I asked in the question

Upvotes: 0

AndrewC
AndrewC

Reputation: 32455

No, splitWhen can't do that for you, because it throws away the delimeters, and you need to keep all the input data:

splitWhen checkEven [1,2,3,4,6,8,9,10] == [[1],[3],[],[],[9],[]]

You can use the Data.List.Split library to do that, because it's very flexible. We can split whenever the element is even, so we use whenElt even:

ghci> :m Data.List.Split
ghci> split (whenElt even) [1,11,2,22,3,33,333,4,6,8,9,99,999,10,100]
[[1,11],[2],[],[22],[3,33,333],[4],[],[6],[],[8],[9,99,999],[10],[],[100],[]]

but we want to condense multiple delimiters like 2,22 into a single list instead of having an empty list of odd numbers in between as we have there, so we use the condense modifier:

ghci> split (condense $ whenElt even) [1,11,2,22,3,33,333,4,6,8,9,99,999,10,100]
[[1,11],[2,22],[3,33,333],[4,6,8],[9,99,999],[10,100],[]]

but let's drop the final blank list that happens because there's another empty list of odds at the end:

ghci> split (dropFinalBlank . condense $ whenElt even) [1,11,2,22,3,33,333,4,6,8,9,99,999,10,100]
[[1,11],[2,22],[3,33,333],[4,6,8],[9,99,999],[10,100]]

If on the other hand you're implementing this from the ground up, then you'll need to use the break function.

Upvotes: 4

Related Questions