Simple haskell splitlist

Posted by js7354 on Stack Overflow See other posts from Stack Overflow or by js7354
Published on 2012-12-09T04:32:06Z Indexed on 2012/12/09 5:04 UTC
Read the original article Hit count: 146

Filed under:

I have the following function which takes a list and returns two sublists split at a given element n. However, I only need to split it in half, with odd length lists having a larger first sublist

splitlist :: [a] -> Int -> ([a],[a])
splitlist [] = ([],[])
splitlist l@(x : xs) n | n > 0     = (x : ys, zs)
               | otherwise = (l, [])
    where (ys,zs) = splitlist xs (n - 1)

I know I need to change the signature to [a] -> ([a],[a]), but where in the code should I put something like length(xs) so that I don't break recursion? Thank you.

© Stack Overflow or respective owner

Related posts about haskell