Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- 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
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement