Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
129 views
in Technique[技术] by (71.8m points)

Haskell file reading

I have just recently started learning Haskell and I am having a lot of trouble trying to figure out how file reading works.

For example, I have a text file "test.txt" containing lines with numbers:

32 4
2 30
300 5

I want to read each line and then evaluate each word and add them.

Thus, I am trying to do something like this:

import System.IO
import Control.Monad

main = do
        let list = []
        handle <- openFile "test.txt" ReadMode
        contents <- hGetContents handle
        singlewords <- (words contents)
        list <- f singlewords
        print list
        hClose handle

f :: [String] -> [Int]
f = map read

I know this is completely wrong, but I don't know how to use the syntax correctly at all.

Any help will be greatly appreciated as well as links to good tutorials that have examples and explanation of code except this one which I have read fully.

question from:https://stackoverflow.com/questions/7867723/haskell-file-reading

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Not a bad start! The only thing to remember is that pure function application should use let instead of the binding <-.

import System.IO  
import Control.Monad

main = do  
        let list = []
        handle <- openFile "test.txt" ReadMode
        contents <- hGetContents handle
        let singlewords = words contents
            list = f singlewords
        print list
        hClose handle   

f :: [String] -> [Int]
f = map read

This is the minimal change needed to get the thing to compile and run. Stylistically, I have a few comments:

  1. Binding list twice looks a bit shady. Note that this isn't mutating the value list -- it's instead shadowing the old definition.
  2. Inline pure functions a lot more!
  3. When possible, using readFile is preferable to manually opening, reading, and closing a file.

Implementing these changes gives something like this:

main = do  
        contents <- readFile "test.txt"
        print . map readInt . words $ contents
-- alternately, main = print . map readInt . words =<< readFile "test.txt"

readInt :: String -> Int
readInt = read

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...