<div dir="ltr">I'm trying to solve problem 22 of Euler Project. (<a href="http://projecteuler.net/problem=22">http://projecteuler.net/problem=22</a>)<div><br></div><div>I got file contents using " names <- readFile "names.txt" " in "do-block".</div>
<div>When I try to use 'names' outside of "do-block", I get following error message;</div><div>namesScores.hs:30:35: Not in scope: `names'</div><div>Failed, modules loaded: none.</div><div><br></div>
<div>Is there any other ways to use variables outside of the "do-block"?</div><div>Sorry for taking your time.</div><div>Thank you.</div><div><br></div><div>My code is...</div><div>-------------------------------------------------------------</div>
<div><div>import Data.List</div><div>import Data.List.Split</div><div>import Data.Char</div><div>import Data.Maybe</div><div><br></div><div>main :: IO()</div><div>main = do</div><div>    names <- readFile "names.txt"</div>
<div>    print $ sum $ map (\x -> getPosition x * getScore x) names</div><div><br></div><div>getPosition :: String -> Int</div><div>getPosition x = fromMaybe 0 (elemIndex x sortedNamesList) + 1</div><div><br></div><div>
getScore :: String -> Int</div><div>getScore xs = sum $ map (\x -> ord x - 64) xs</div><div><br></div><div>sortedNamesList :: [String]</div><div>sortedNamesList = sort $ nameList names</div><div><br></div><div>nameList :: String -> [String]</div>
<div>nameList = split (dropDelims . dropBlanks $ oneOf ",\"")</div></div><div><br></div><div><br></div><div>Error message is...</div><div>------------------------------------------------------------------</div>
<div><div>Prelude> :l namesScores.hs</div><div>[1 of 1] Compiling Main             ( namesScores.hs, interpreted )</div><div><br></div><div>namesScores.hs:31:35: Not in scope: `names'</div><div>Failed, modules loaded: none.</div>
</div></div>