Advertisement
Guest User

Untitled

a guest
Oct 12th, 2017
65
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.08 KB | None | 0 0
  1. import Data.Char(isSpace)
  2.  
  3. -- splitOnTest (=='x') "AxBxC" -> ["A", "B", "C"]
  4. splitOnTest :: (Char -> Bool) -> String -> [String]
  5. splitOnTest = splitOnTest' []
  6. where splitOnTest' :: String -> (Char -> Bool) -> String -> [String]
  7. splitOnTest' acc _ [] = [(reverse acc)]
  8. splitOnTest' acc f (x:xs) | f x = [(reverse acc)] ++ splitOnTest f xs
  9. | otherwise = splitOnTest' (x:acc) f xs
  10.  
  11. -- splitOn 'x' "AxBxC" -> ["A", "B", "C"]
  12. splitOn :: Char -> String -> [String]
  13. splitOn ch = splitOnTest (==ch)
  14.  
  15. -- strip " A " -> "A"
  16. strip :: String -> String
  17. strip = reverse . dropWhile isSpace . reverse . dropWhile isSpace
  18.  
  19. stringList :: [String] -> [(String, [Float])]
  20. stringList s = stringList' [] $ map (map strip) $ map (splitOn ',') s
  21. where stringList' acc [] = reverse acc
  22. stringList' acc (x:xs) = stringList' ((s, ns):acc) xs
  23. where (s:ns') = x
  24. ns = map (read :: String -> Float) ns'
  25.  
  26. testData = ["Adam, 3, 5, 2.3",
  27. "George, 20, 50.3, 100.0"]
  28.  
  29. main = putStrLn $ unlines $ map show $ stringList testData
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement