ReadCSV.hs 502 Bytes
Newer Older
pm3g19's avatar
pm3g19 committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
module ReadCSV where

import System.IO

readCSV :: FilePath -> IO [[String]]
readCSV fname = do
    str <- readFile fname
    return $ readCSVString str

readCSVString :: String -> [[String]]
readCSVString whole = [splitElem ',' line | line <- splitElem '\n' whole]
splitElem :: Eq a => a -> [a] -> [[a]]
splitElem elem = split (/=elem)

split :: (a -> Bool) -> [a] -> [[a]]
split p l = case span p l of
        ([], _) -> []
        (match, []) -> [match]
        (match, _:rem') -> match:split p rem'