[Haskell-beginners] About scope range: outside of the do-block

S. H. Aegis shaegis at gmail.com
Sun Apr 20 12:57:58 UTC 2014


I'm trying to solve problem 22 of Euler Project. (
http://projecteuler.net/problem=22)

I got file contents using " names <- readFile "names.txt" " in "do-block".
When I try to use 'names' outside of "do-block", I get following error
message;
namesScores.hs:30:35: Not in scope: `names'
Failed, modules loaded: none.

Is there any other ways to use variables outside of the "do-block"?
Sorry for taking your time.
Thank you.

My code is...
-------------------------------------------------------------
import Data.List
import Data.List.Split
import Data.Char
import Data.Maybe

main :: IO()
main = do
    names <- readFile "names.txt"
    print $ sum $ map (\x -> getPosition x * getScore x) names

getPosition :: String -> Int
getPosition x = fromMaybe 0 (elemIndex x sortedNamesList) + 1

getScore :: String -> Int
getScore xs = sum $ map (\x -> ord x - 64) xs

sortedNamesList :: [String]
sortedNamesList = sort $ nameList names

nameList :: String -> [String]
nameList = split (dropDelims . dropBlanks $ oneOf ",\"")


Error message is...
------------------------------------------------------------------
Prelude> :l namesScores.hs
[1 of 1] Compiling Main             ( namesScores.hs, interpreted )

namesScores.hs:31:35: Not in scope: `names'
Failed, modules loaded: none.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://www.haskell.org/pipermail/beginners/attachments/20140420/9acc12c5/attachment.html>


More information about the Beginners mailing list