Stefan Flondor
Stefan Flondor

Reputation: 369

haskell read from file

i'm trying to read from file in haskell and process every line what i have now

main = interact (unlines . (map calculate) . lines)

this let me get every line from input and send it to calculate

now i want to get every line from a file and send it to calculate

this is what i tried

main = do 
    text <- readFile "input.txt"
    let linii = lines text
    interact (unlines . (map calculate) . linii)

tell me please how is it correct?

UPDATE below

calculate :: String -> String
calculate s=
    case ret of
        Left e -> "error: " ++(show e)
        Right n -> "answer: " ++ (show n)
    where
        ret = parse parseInput "" s

main :: IO()
--main = interact (unlines . (map calculate) . lines)
main = do text <- readFile "input.txt"
          let linii = lines
          putStrLn . unlines $ map calculate linii

Upvotes: 3

Views: 1429

Answers (1)

Code-Apprentice
Code-Apprentice

Reputation: 83527

Remember that interact takes input from stdin and sends output to stdout. Since you have already read input from a file, you don't need the former. You only need to do the later. You can print a String with putStrln. Putting this all together, change

interact (unlines . (map calculate) . linii)

to

putStrLn . unlines $ map calculate linii

Upvotes: 7

Related Questions