alea/List.hs

17 lines
455 B
Haskell
Raw Normal View History

2014-10-13 21:26:55 +02:00
module List where
-- Remove the nth element of a list. (0-indexed)
-- RemoveAt 2 "abc" == ('c', "ab")
removeAt :: Int -> [a] -> (a, [a])
removeAt n xs = (xs !! n, take n xs ++ drop (n+1) xs)
-- Split a string into a list of strings
-- ex. split "ab,cd,ef" ',' == ["ab","cd","ef"]
split :: String -> Char -> [String]
split [] _ = [""]
split (c:cs) delim
| c == delim = "" : rest
| otherwise = (c : head rest) : tail rest
where
rest = split cs delim