Scanning CSV files

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
module Control.Scan.CSV where

-- I do this often enough: scanning in a CSV file, so, here it is:

-- your words function with a 'by'-argument. Should be there already ...

wordsBy :: String -> String -> [String]
wordsBy delims line =
   let isDelim = flip elem delims
   in  case dropWhile isDelim line of
            "" -> []       
            s' -> w : wordsBy delims s''
               where (w, s'') = break isDelim s'

csv :: String -> [String]
csv = wordsBy "," -- so we could just write this.