Created
February 3, 2018 11:42
-
-
Save prl900/c532fc95f844d50ab254622c42f0e558 to your computer and use it in GitHub Desktop.
Haskell CSV parser sample code
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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