Advertisement
Guest User

Untitled

a guest
Jun 26th, 2019
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.85 KB | None | 0 0
  1. 5.
  2. 7.
  3. [(1,2,3),(4,5,6),(7,8,9),(10,11,12)].
  4.  
  5. digitToInt :: String -> Int
  6.  
  7. getLine :: IO String
  8.  
  9. readFile :: FilePath -> IO String
  10.  
  11. lines:: String -> [String]
  12.  
  13. init :: [a] -> [a]
  14.  
  15. read :: (Read a) => String -> a
  16.  
  17. run :: IO (Int, Int, [(Int,Int,Int)])
  18. run = do
  19. contents <- readFile "text.txt" -- use '<-' here so that 'contents' is a String
  20. let [a,b,c] = lines contents -- split on newlines
  21. let firstLine = read (init a) -- 'init' drops the trailing period
  22. let secondLine = read (init b)
  23. let thirdLine = read (init c) -- this reads a list of Int-tuples
  24. return (firstLine, secondLine, thirdLine)
  25.  
  26. line <- getLine
  27.  
  28. :type line
  29. line :: String
  30.  
  31. :type getLine
  32. getLine :: IO String
  33.  
  34. main = do
  35. putStrLn "How much do you love Haskell?"
  36. amount <- getLine
  37. putStrln ("You love Haskell this much: " ++ amount)
  38.  
  39. :type readFile
  40. readFile :: FilePath -> IO String
  41.  
  42. yourdata <- readFile "samplefile.txt"
  43.  
  44. :type yourdata
  45. text :: String
  46.  
  47. fmap doTheRightThingWithString readStringFromFile
  48.  
  49. import Control.Applicative
  50.  
  51. ...
  52.  
  53. doTheRightThingWithString <$> readStringFromFile
  54.  
  55. import Control.Monad
  56.  
  57. ...
  58.  
  59. liftM doTheRightThingWithString readStringFromFile
  60.  
  61. readStringFromFile >>= string -> return (doTheRightThingWithString string)
  62. readStringFromFile >>= string -> return $ doTheRightThingWithString string
  63. readStringFromFile >>= return . doTheRightThingWithString
  64. return . doTheRightThingWithString =<< readStringFromFile
  65.  
  66. do
  67. ...
  68. string <- readStringFromFile
  69. -- ^ you escape String from IO but only inside this do-block
  70. let result = doTheRightThingWithString string
  71. ...
  72. return result
  73.  
  74. myParser :: String -> Foo
  75.  
  76. readFile "thisfile.txt"
  77.  
  78. fmap myParser (readFile "thisfile.txt")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement