About scope range: outside of the do-block

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
6 messages Options
Reply | Threaded
Open this post in threaded view
|

About scope range: outside of the do-block

S. Chang
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>

Reply | Threaded
Open this post in threaded view
|

About scope range: outside of the do-block

Roel van Dijk-3
On 20 April 2014 14:57, S. H. Aegis <shaegis at gmail.com> wrote:
>
>  Is there any other ways to use variables outside of the "do-block"?
>

You can pass the list of names as an argument where it is needed.


> 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 names x * getScore x) names
>
> getPosition :: [String] -> String -> Int
> getPosition names x = fromMaybe 0 (elemIndex x $ sortedNamesList names) + 1
>
> getScore :: String -> Int
> getScore xs = sum $ map (\x -> ord x - 64) xs
>
> sortedNamesList :: [String] -> [String]
> sortedNamesList names = sort $ nameList names
>
> nameList :: String -> [String]
> nameList = split (dropDelims . dropBlanks $ oneOf ",\"")
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://www.haskell.org/pipermail/beginners/attachments/20140420/63492b5e/attachment.html>

Reply | Threaded
Open this post in threaded view
|

About scope range: outside of the do-block

Norbert Melzer
In reply to this post by S. Chang
Am 20.04.2014 14:58 schrieb "S. H. Aegis" <shaegis at gmail.com>:

> namesScores.hs:31:35: Not in scope: `names'
> Failed, modules loaded: none.

The code you posted has only 21 lines while your error message denotes an
error in line 31, so there is something missing.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://www.haskell.org/pipermail/beginners/attachments/20140420/a5c0c403/attachment.html>

Reply | Threaded
Open this post in threaded view
|

About scope range: outside of the do-block

S. Chang
Oh, I'm sorry.
I delete some of commented lines.
Sorry for confusing.

Error occurred in
sortedNamesList = sort $ nameList names

Thank you.


2014-04-20 22:17 GMT+09:00 Norbert Melzer <timmelzer at gmail.com>:

>
> Am 20.04.2014 14:58 schrieb "S. H. Aegis" <shaegis at gmail.com>:
>
> > namesScores.hs:31:35: Not in scope: `names'
> > Failed, modules loaded: none.
>
> The code you posted has only 21 lines while your error message denotes an
> error in line 31, so there is something missing.
>
> _______________________________________________
> Beginners mailing list
> Beginners at haskell.org
> http://www.haskell.org/mailman/listinfo/beginners
>
>


--
Sok Ha, CHANG
Dr. Chang's Clinic. #203. 503-23. AmSa-Dong, GangDong-Gu, Seoul.
Tel: +82-2-442-7585
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://www.haskell.org/pipermail/beginners/attachments/20140420/9570e7f0/attachment.html>

Reply | Threaded
Open this post in threaded view
|

About scope range: outside of the do-block

S. Chang
In reply to this post by Roel van Dijk-3
It works !
Thank you so much !
Have a nice day~




2014-04-20 22:09 GMT+09:00 Roel van Dijk <vandijk.roel at gmail.com>:

> On 20 April 2014 14:57, S. H. Aegis <shaegis at gmail.com> wrote:
>>
>>  Is there any other ways to use variables outside of the "do-block"?
>>
>
> You can pass the list of names as an argument where it is needed.
>
>
>> 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 names x * getScore x) names
>>
>> getPosition :: [String] -> String -> Int
>> getPosition names x = fromMaybe 0 (elemIndex x $ sortedNamesList names) +
>> 1
>>
>> getScore :: String -> Int
>> getScore xs = sum $ map (\x -> ord x - 64) xs
>>
>> sortedNamesList :: [String] -> [String]
>> sortedNamesList names = sort $ nameList names
>>
>> nameList :: String -> [String]
>> nameList = split (dropDelims . dropBlanks $ oneOf ",\"")
>>
>
>
>
> _______________________________________________
> Beginners mailing list
> Beginners at haskell.org
> http://www.haskell.org/mailman/listinfo/beginners
>
>


--
Sok Ha, CHANG
Dr. Chang's Clinic. #203. 503-23. AmSa-Dong, GangDong-Gu, Seoul.
Tel: +82-2-442-7585
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://www.haskell.org/pipermail/beginners/attachments/20140420/452b2234/attachment-0001.html>

Reply | Threaded
Open this post in threaded view
|

About scope range: outside of the do-block

Gesh
In reply to this post by S. Chang
On April 20, 2014 4:38:05 PM GMT+03:00, "S. H. Aegis" <shaegis at gmail.com> wrote:

>Oh, I'm sorry.
>I delete some of commented lines.
>Sorry for confusing.
>
>Error occurred in
>sortedNamesList = sort $ nameList names
>
>Thank you.
>
>
>2014-04-20 22:17 GMT+09:00 Norbert Melzer <timmelzer at gmail.com>:
>
>>
>> Am 20.04.2014 14:58 schrieb "S. H. Aegis" <shaegis at gmail.com>:
>>
>> > namesScores.hs:31:35: Not in scope: `names'
>> > Failed, modules loaded: none.
>>
>> The code you posted has only 21 lines while your error message
>denotes an
>> error in line 31, so there is something missing.
>>
>> _______________________________________________
>> Beginners mailing list
>> Beginners at haskell.org
>> http://www.haskell.org/mailman/listinfo/beginners
>>
>>

In general, variables defined locally in one scope are not floated to external scopes.
Thus, your question is analogous to asking:
Given the following code:
foo = ...
  where bar = ...
baz = ...
Can I call bar from baz? The answer being no, you can't, unless you pass bar as a parameter to baz (or use a Reader monad, which is equivalent).

Hoping to help,
Gesh