haskell - Why this variable is invisible in where -
i want read matrix [[int]] text file(this matrix given in project euler's problem), have following code
parseint :: string -> [int] parseint [] = [] parseint (x : xs) = [(ord x) - (ord '0')] ++ (parseint xs) main = str <- readfile "11.dat" print $ fmap parseint (lines str) this code works fine , can output matrix read.
however, want change main function, can reuse fmap parseint (lines str) instead of repeating in code.
main = str <- readfile "11.dat" print b b = fmap parseint (lines str) the compiler gives me error
11.hs:37:34: error: variable not in scope: str :: string [finished in 0.9s] it seems feed operation str <- readfile "11.dat" causes problem because when read string directly code works fine
main = print b 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 let
main = str <- readfile "11.dat" let b = fmap parseint (lines str) print b so how can that
this parsing issue. haskell parses offending code as
main = (do {str <- readfile "11.dat"; print b}) {b = fmap parseint (lines str)} so local variables in scope where clause pattern variables left of = (all none of them, in general, might have some).
meanwhile str scopes binding end of do block. , that's why putting let in do block after binding works fine.
Comments
Post a Comment