Last active
October 11, 2017 23:34
-
-
Save NotTheEconomist/6e129c9f015d39100ccaa4079f140f64 to your computer and use it in GitHub Desktop.
This file contains 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
import Data.Char(isSpace) | |
-- splitOnTest (=='x') "AxBxC" -> ["A", "B", "C"] | |
splitOnTest :: (Char -> Bool) -> String -> [String] | |
splitOnTest = splitOnTest' [] | |
where splitOnTest' :: String -> (Char -> Bool) -> String -> [String] | |
splitOnTest' acc _ [] = [(reverse acc)] | |
splitOnTest' acc f (x:xs) | f x = [(reverse acc)] ++ splitOnTest f xs | |
| otherwise = splitOnTest' (x:acc) f xs | |
-- splitOn 'x' "AxBxC" -> ["A", "B", "C"] | |
splitOn :: Char -> String -> [String] | |
splitOn ch = splitOnTest (==ch) | |
-- strip " A " -> "A" | |
strip :: String -> String | |
strip = reverse . dropWhile isSpace . reverse . dropWhile isSpace | |
stringList :: [String] -> [(String, [Float])] | |
stringList s = stringList' [] $ map (map strip) $ map (splitOn ',') s | |
where stringList' acc [] = reverse acc | |
stringList' acc (x:xs) = stringList' ((s, ns):acc) xs | |
where (s:ns') = x | |
ns = map (read :: String -> Float) ns' | |
testData = ["Adam, 3, 5, 2.3", | |
"George, 20, 50.3, 100.0"] | |
main = putStrLn $ unlines $ map show $ stringList testData |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment