Skip to content

Instantly share code, notes, and snippets.

@prl900
Created February 3, 2018 11:42
Show Gist options
  • Save prl900/c532fc95f844d50ab254622c42f0e558 to your computer and use it in GitHub Desktop.
Save prl900/c532fc95f844d50ab254622c42f0e558 to your computer and use it in GitHub Desktop.
Haskell CSV parser sample code
split :: String -> [String]
split [] = [""]
split (c:cs) | c == ',' = "" : rest
| otherwise = (c : head rest) : tail rest
where rest = split cs
nameexists :: String -> [[String]] -> Bool
nameexists a [] = False
nameexists a (x:xs) | x !! 0 == a = True
| otherwise = nameexists a xs
getphone :: String -> [[String]] -> String
getphone a [] = ""
getphone a (x:xs) | x !! 0 == a = x !! 1
| otherwise = getphone a xs
main = do
filecontent <- readFile "CSVFile.csv"
let phonebook = (map split (tail (lines (filter (/= '\r') filecontent))))
print ((length phonebook == 4) && (nameexists "Bob" phonebook) && (getphone "Dave" phonebook == "0400 001 123"))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment