I want to read a matrix [[Int]]
from a text file(this matrix is given in
a project euler's problem), so I have the following code
parseInt :: String -> [Int]
parseInt [] = []
parseInt (x : xs) = [(ord x) - (ord '0')] ++ (parseInt xs)
main = do
str <- readFile "11.dat"
print $ fmap parseInt (lines str)
this code works fine and I can output the matrix a read.
However, I want to change the main
function, so I can reuse fmap parseInt (lines str)
instead of repeating it in my code.
main = do
str <- readFile "11.dat"
print b
where b = fmap parseInt (lines str)
the compiler gives me an error
11.hs:37:34: error:
Variable not in scope: str :: String
[Finished in 0.9s]
It seems that the feed operation str <- readFile "11.dat"
causes this problem because when I read from a string directly the code works fine
main = do
print b
where b = fmap parseInt (lines "08 02 22 97 38 15 00 40 00 75 04 05 07 78 52 12 50 77 91 08\n...01 70 54 71 83 51 54 69 16 92 33 48 61 43 52 01 89 19 67 48\n")
I can also with let
main = do
str <- readFile "11.dat"
let b = fmap parseInt (lines str)
print b
So how can I do that with that