How is this variable not in scope?

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

How is this variable not in scope?

Jeffrey Brown
The following compiles and runs perfectly:

main = do
  (inputFile:_)  <- getArgs
  input <- readFile inputFile
  let pairs = pairFilesToContents $ readSmsnLines input
  mapM_ f pairs where
    f :: (FilePath, String) -> IO ()
    f (name, content) = writeFile name content

But the following triggers the error "Not in scope: ‘root’".

main' = do
  (root : inputFile :_)  <- getArgs
  input <- readFile inputFile
  let pairs = pairFilesToContents $ readSmsnLines input
  mapM_ f pairs where
    f :: (FilePath, String) -> IO ()
    f (name, content) = writeFile (root+name) content

--
Jeff Brown | Jeffrey Benjamin Brown
Website   |   Facebook   |   LinkedIn(spammy, so I often miss messages here)   |   Github   

_______________________________________________
Haskell-Cafe mailing list
To (un)subscribe, modify options or view archives go to:
http://mail.haskell.org/cgi-bin/mailman/listinfo/haskell-cafe
Only members subscribed via the mailman list are allowed to post.
Reply | Threaded
Open this post in threaded view
|

Re: How is this variable not in scope?

Alejandro Serrano Mena
The problem is that in the "where" clause the only things which are in scope are (roughly) the arguments of the function. In particular, neither "root" nor "inputFile" nor "input" nor "pairs". The solution is to add an extra argument:

main' = do
  ...
  mapM_ (f root) pairs where
    f root (name, content) = writeFile (root ++ name) content

2017-07-21 9:28 GMT+02:00 Jeffrey Brown <[hidden email]>:
The following compiles and runs perfectly:

main = do
  (inputFile:_)  <- getArgs
  input <- readFile inputFile
  let pairs = pairFilesToContents $ readSmsnLines input
  mapM_ f pairs where
    f :: (FilePath, String) -> IO ()
    f (name, content) = writeFile name content

But the following triggers the error "Not in scope: ‘root’".

main' = do
  (root : inputFile :_)  <- getArgs
  input <- readFile inputFile
  let pairs = pairFilesToContents $ readSmsnLines input
  mapM_ f pairs where
    f :: (FilePath, String) -> IO ()
    f (name, content) = writeFile (root+name) content

--
Jeff Brown | Jeffrey Benjamin Brown
Website   |   Facebook   |   LinkedIn(spammy, so I often miss messages here)   |   Github   

_______________________________________________
Haskell-Cafe mailing list
To (un)subscribe, modify options or view archives go to:
http://mail.haskell.org/cgi-bin/mailman/listinfo/haskell-cafe
Only members subscribed via the mailman list are allowed to post.


_______________________________________________
Haskell-Cafe mailing list
To (un)subscribe, modify options or view archives go to:
http://mail.haskell.org/cgi-bin/mailman/listinfo/haskell-cafe
Only members subscribed via the mailman list are allowed to post.
Reply | Threaded
Open this post in threaded view
|

Re: How is this variable not in scope?

Jeffrey Brown
Excellent! Thanks, Alejandro!

On Fri, Jul 21, 2017 at 12:52 AM, Alejandro Serrano Mena <[hidden email]> wrote:
The problem is that in the "where" clause the only things which are in scope are (roughly) the arguments of the function. In particular, neither "root" nor "inputFile" nor "input" nor "pairs". The solution is to add an extra argument:

main' = do
  ...
  mapM_ (f root) pairs where
    f root (name, content) = writeFile (root ++ name) content

2017-07-21 9:28 GMT+02:00 Jeffrey Brown <[hidden email]>:
The following compiles and runs perfectly:

main = do
  (inputFile:_)  <- getArgs
  input <- readFile inputFile
  let pairs = pairFilesToContents $ readSmsnLines input
  mapM_ f pairs where
    f :: (FilePath, String) -> IO ()
    f (name, content) = writeFile name content

But the following triggers the error "Not in scope: ‘root’".

main' = do
  (root : inputFile :_)  <- getArgs
  input <- readFile inputFile
  let pairs = pairFilesToContents $ readSmsnLines input
  mapM_ f pairs where
    f :: (FilePath, String) -> IO ()
    f (name, content) = writeFile (root+name) content

--
Jeff Brown | Jeffrey Benjamin Brown
Website   |   Facebook   |   LinkedIn(spammy, so I often miss messages here)   |   Github   

_______________________________________________
Haskell-Cafe mailing list
To (un)subscribe, modify options or view archives go to:
http://mail.haskell.org/cgi-bin/mailman/listinfo/haskell-cafe
Only members subscribed via the mailman list are allowed to post.




--
Jeff Brown | Jeffrey Benjamin Brown
Website   |   Facebook   |   LinkedIn(spammy, so I often miss messages here)   |   Github   

_______________________________________________
Haskell-Cafe mailing list
To (un)subscribe, modify options or view archives go to:
http://mail.haskell.org/cgi-bin/mailman/listinfo/haskell-cafe
Only members subscribed via the mailman list are allowed to post.
Reply | Threaded
Open this post in threaded view
|

Re: How is this variable not in scope?

Adam Bergmark-2
Another solution is to define `f` below `root` within the do block:

```
main' = do
  (root : inputFile :_)  <- getArgs
  input <- readFile inputFile
  let pairs = pairFilesToContents $ readSmsnLines input
  let f :: (FilePath, String) -> IO ()
       f (name, content) = writeFile (root+name) content  
  mapM_ f pairs
```


On Fri, 21 Jul 2017 at 10:07 Jeffrey Brown <[hidden email]> wrote:
Excellent! Thanks, Alejandro!

On Fri, Jul 21, 2017 at 12:52 AM, Alejandro Serrano Mena <[hidden email]> wrote:
The problem is that in the "where" clause the only things which are in scope are (roughly) the arguments of the function. In particular, neither "root" nor "inputFile" nor "input" nor "pairs". The solution is to add an extra argument:

main' = do
  ...
  mapM_ (f root) pairs where
    f root (name, content) = writeFile (root ++ name) content

2017-07-21 9:28 GMT+02:00 Jeffrey Brown <[hidden email]>:
The following compiles and runs perfectly:

main = do
  (inputFile:_)  <- getArgs
  input <- readFile inputFile
  let pairs = pairFilesToContents $ readSmsnLines input
  mapM_ f pairs where
    f :: (FilePath, String) -> IO ()
    f (name, content) = writeFile name content

But the following triggers the error "Not in scope: ‘root’".

main' = do
  (root : inputFile :_)  <- getArgs
  input <- readFile inputFile
  let pairs = pairFilesToContents $ readSmsnLines input
  mapM_ f pairs where
    f :: (FilePath, String) -> IO ()
    f (name, content) = writeFile (root+name) content

--
Jeff Brown | Jeffrey Benjamin Brown
Website   |   Facebook   |   LinkedIn(spammy, so I often miss messages here)   |   Github   

_______________________________________________
Haskell-Cafe mailing list
To (un)subscribe, modify options or view archives go to:
http://mail.haskell.org/cgi-bin/mailman/listinfo/haskell-cafe
Only members subscribed via the mailman list are allowed to post.




--
Jeff Brown | Jeffrey Benjamin Brown
Website   |   Facebook   |   LinkedIn(spammy, so I often miss messages here)   |   Github   
_______________________________________________
Haskell-Cafe mailing list
To (un)subscribe, modify options or view archives go to:
http://mail.haskell.org/cgi-bin/mailman/listinfo/haskell-cafe
Only members subscribed via the mailman list are allowed to post.

_______________________________________________
Haskell-Cafe mailing list
To (un)subscribe, modify options or view archives go to:
http://mail.haskell.org/cgi-bin/mailman/listinfo/haskell-cafe
Only members subscribed via the mailman list are allowed to post.