Skip to content

Instantly share code, notes, and snippets.

@NotTheEconomist
Last active October 11, 2017 23:34
Show Gist options
  • Save NotTheEconomist/6e129c9f015d39100ccaa4079f140f64 to your computer and use it in GitHub Desktop.
Save NotTheEconomist/6e129c9f015d39100ccaa4079f140f64 to your computer and use it in GitHub Desktop.
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