IO String and String using readFile

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

IO String and String using readFile

Yugesh Kothari
Hi, 

I am trying to read data from a file.
When I do-
ghci> contents <- readFile "file.txt"
ghci> words contents

Then everything works.

The same thing when done in a .hs file gives an IO String vs [String] error.

Why is that so?

_______________________________________________
Beginners mailing list
[hidden email]
http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners
Reply | Threaded
Open this post in threaded view
|

Re: IO String and String using readFile

Ian Denhardt
It would help to see the complete source file, but I'll hazard a guess
you're doing something like:

main = do
    contents <- readFile "file.txt"
    words contents

At the GHCi prompt, each line can be any expression, and GHCi will
evaluate it and then display it. The two lines in your session aren't
really related.

In contrast, the do block in the source file expects the expression at
the end to be an IO action.

What do you want your program to do when you get to "words contents"?
display it? (If you want to display it, pass it to `print`).

Quoting Yugesh Kothari (2019-03-26 23:35:14)
>    Hi,�
>    I am trying to read data from a file.
>    When I do-
>    ghci> contents <- readFile "file.txt"
>    ghci> words contents
>    Then everything works.
>    The same thing when done in a .hs file gives an IO String vs [String]
>    error.
>    Why is that so?
_______________________________________________
Beginners mailing list
[hidden email]
http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners
Reply | Threaded
Open this post in threaded view
|

Re: IO String and String using readFile

Yugesh Kothari
I see. anyway, my use case is something like this-

I have two functions
fromFile :: [String] -> [Int]
func :: String -> [Int]

I want to use the "words contents" output and pass each element in it to func and send back [Int] from "fromFile" function (where I originally read the file.)

Could you suggest a better way to do this?

Quoting Ian Denhardt


On Wed, 27 Mar, 2019, 9:13 AM Ian Denhardt, <[hidden email]> wrote:
It would help to see the complete source file, but I'll hazard a guess
you're doing something like:

main = do
    contents <- readFile "file.txt"
    words contents

At the GHCi prompt, each line can be any expression, and GHCi will
evaluate it and then display it. The two lines in your session aren't
really related.

In contrast, the do block in the source file expects the expression at
the end to be an IO action.

What do you want your program to do when you get to "words contents"?
display it? (If you want to display it, pass it to `print`).

Quoting Yugesh Kothari (2019-03-26 23:35:14)
>    Hi,�
>    I am trying to read data from a file.
>    When I do-
>    ghci> contents <- readFile "file.txt"
>    ghci> words contents
>    Then everything works.
>    The same thing when done in a .hs file gives an IO String vs [String]
>    error.
>    Why is that so?

_______________________________________________
Beginners mailing list
[hidden email]
http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners
Reply | Threaded
Open this post in threaded view
|

Re: IO String and String using readFile

Theodore Lief Gannon
In reply to this post by Yugesh Kothari
I'm guessing in the .hs file, you have this in a do block? Something like:

foo = do
    contents <- readFile
    words contents

If so, the problem is the return type of `words`. Each line of the do block has to have an IO value, and words is a pure function. To lift its result into the monad, use the aptly-named `pure` function:

foo = do
    contents <- readFile
    pure $ words contents


On Tue, Mar 26, 2019, 8:35 PM Yugesh Kothari <[hidden email]> wrote:
Hi, 

I am trying to read data from a file.
When I do-
ghci> contents <- readFile "file.txt"
ghci> words contents

Then everything works.

The same thing when done in a .hs file gives an IO String vs [String] error.

Why is that so?
_______________________________________________
Beginners mailing list
[hidden email]
http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners

_______________________________________________
Beginners mailing list
[hidden email]
http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners
Reply | Threaded
Open this post in threaded view
|

Fwd: IO String and String using readFile

Yugesh Kothari
In reply to this post by Yugesh Kothari
Extremely sorry,

The definition of fromFile is String -> [Int] where input parameter is the name of the file.

---------- Forwarded message ---------
From: Yugesh Kothari <[hidden email]>
Date: Wed, 27 Mar, 2019, 9:18 AM
Subject: Re: [Haskell-beginners] IO String and String using readFile
To:
Cc: <[hidden email]>


I see. anyway, my use case is something like this-

I have two functions
fromFile :: [String] -> [Int]
func :: String -> [Int]

I want to use the "words contents" output and pass each element in it to func and send back [Int] from "fromFile" function (where I originally read the file.)

Could you suggest a better way to do this?

Quoting Ian Denhardt


On Wed, 27 Mar, 2019, 9:13 AM Ian Denhardt, <[hidden email]> wrote:
It would help to see the complete source file, but I'll hazard a guess
you're doing something like:

main = do
    contents <- readFile "file.txt"
    words contents

At the GHCi prompt, each line can be any expression, and GHCi will
evaluate it and then display it. The two lines in your session aren't
really related.

In contrast, the do block in the source file expects the expression at
the end to be an IO action.

What do you want your program to do when you get to "words contents"?
display it? (If you want to display it, pass it to `print`).

Quoting Yugesh Kothari (2019-03-26 23:35:14)
>    Hi,�
>    I am trying to read data from a file.
>    When I do-
>    ghci> contents <- readFile "file.txt"
>    ghci> words contents
>    Then everything works.
>    The same thing when done in a .hs file gives an IO String vs [String]
>    error.
>    Why is that so?

_______________________________________________
Beginners mailing list
[hidden email]
http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners
Reply | Threaded
Open this post in threaded view
|

Re: IO String and String using readFile

Ian Denhardt
In reply to this post by Yugesh Kothari
Sounds like you want concatMap:

    https://hackage.haskell.org/package/base-4.12.0.0/docs/Prelude.html#v:concatMap

Quoting Yugesh Kothari (2019-03-26 23:48:35)

>    I see. anyway, my use case is something like this-
>    I have two functions
>    fromFile :: [String] -> [Int]
>    func :: String -> [Int]
>    I want to use the "words contents" output and pass each element in it
>    to func and send back [Int] from "fromFile" function (where I
>    originally read the file.)
>    Could you suggest a better way to do this?
>    Quoting� Ian Denhardt
>    On Wed, 27 Mar, 2019, 9:13 AM Ian Denhardt, <[1][hidden email]> wrote:
>
>      It would help to see the complete source file, but I'll hazard a
>      guess
>      you're doing something like:
>      main = do
>      �  �  contents <- readFile "file.txt"
>      �  �  words contents
>      At the GHCi prompt, each line can be any expression, and GHCi will
>      evaluate it and then display it. The two lines in your session
>      aren't
>      really related.
>      In contrast, the do block in the source file expects the expression
>      at
>      the end to be an IO action.
>      What do you want your program to do when you get to "words
>      contents"?
>      display it? (If you want to display it, pass it to `print`).
>      Quoting Yugesh Kothari (2019-03-26 23:35:14)
>      >�  �  Hi,�
>      >�  �  I am trying to read data from a file.
>      >�  �  When I do-
>      >�  �  ghci> contents <- readFile "file.txt"
>      >�  �  ghci> words contents
>      >�  �  Then everything works.
>      >�  �  The same thing when done in a .hs file gives an IO String vs
>      [String]
>      >�  �  error.
>      >�  �  Why is that so?
>
> Verweise
>
>    1. mailto:[hidden email]
_______________________________________________
Beginners mailing list
[hidden email]
http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners
Reply | Threaded
Open this post in threaded view
|

Re: Fwd: IO String and String using readFile

Ian Denhardt
In reply to this post by Yugesh Kothari
Ah, in that case you'll need the type to be

    String -> IO [Int]

In Haskell, functions *only* do computation -- no side effects, like
reading a file. So a String -> [Int] can't do what you want. We have a
separate type, IO, for doing things that affect the outside world. For
example:

ghci> :type readFile
readFile :: FilePath -> IO String

Also, just a style tip: there's a type alias for String defined called
FilePath, which would make the type signature more clear in this case.

Quoting Yugesh Kothari (2019-03-26 23:54:40)

>    Extremely sorry,
>    The definition of fromFile is String -> [Int] where input parameter is
>    the name of the file.
>    ---------- Forwarded message ---------
>    From: Yugesh Kothari <[1][hidden email]>
>    Date: Wed, 27 Mar, 2019, 9:18 AM
>    Subject: Re: [Haskell-beginners] IO String and String using readFile
>    To:
>    Cc: <[2][hidden email]>
>    I see. anyway, my use case is something like this-
>    I have two functions
>    fromFile :: [String] -> [Int]
>    func :: String -> [Int]
>    I want to use the "words contents" output and pass each element in it
>    to func and send back [Int] from "fromFile" function (where I
>    originally read the file.)
>    Could you suggest a better way to do this?
>    Quoting� Ian Denhardt
>    On Wed, 27 Mar, 2019, 9:13 AM Ian Denhardt, <[3][hidden email]> wrote:
>
>      It would help to see the complete source file, but I'll hazard a
>      guess
>      you're doing something like:
>      main = do
>      �  �  contents <- readFile "file.txt"
>      �  �  words contents
>      At the GHCi prompt, each line can be any expression, and GHCi will
>      evaluate it and then display it. The two lines in your session
>      aren't
>      really related.
>      In contrast, the do block in the source file expects the expression
>      at
>      the end to be an IO action.
>      What do you want your program to do when you get to "words
>      contents"?
>      display it? (If you want to display it, pass it to `print`).
>      Quoting Yugesh Kothari (2019-03-26 23:35:14)
>      >�  �  Hi,�
>      >�  �  I am trying to read data from a file.
>      >�  �  When I do-
>      >�  �  ghci> contents <- readFile "file.txt"
>      >�  �  ghci> words contents
>      >�  �  Then everything works.
>      >�  �  The same thing when done in a .hs file gives an IO String vs
>      [String]
>      >�  �  error.
>      >�  �  Why is that so?
>
> Verweise
>
>    1. mailto:[hidden email]
>    2. mailto:[hidden email]
>    3. mailto:[hidden email]
_______________________________________________
Beginners mailing list
[hidden email]
http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners