/ Published in: Haskell
problem 17, Split a list into two parts; the length of the first part is given.
Do not use any predefined predicates. (Meaning no splitAt or take or drop)
*Main> split "abcdefghik" 3
("abc", "defghik")
Do not use any predefined predicates. (Meaning no splitAt or take or drop)
*Main> split "abcdefghik" 3
("abc", "defghik")
Expand |
Embed | Plain Text
Copy this code and paste it in your HTML
-- Introducing my convoluted implementation of splitAt split xs n = (pre xs 0 n, post xs 0 (n-1)) pre [] _ _ = [] pre xs dex n then xs!!dex : pre xs (dex+1) n else [] post [] _ _ = [] post (x:xs) dex n | n < 0 = (x:xs) then post xs (dex+1) n else xs -- Or, to borrow from the solutions on Haskell.org -- (which is incorrectfor negative n, BTW) split' xs 0 = ([], xs) split' (x:xs) n = let (f,l) = split xs (n-1) in (x : f, l)
URL: http://haskell.org/haskellwiki/99_questions/11_to_20