[Haskell-beginners] Fwd: IO String and String using readFile

Yugesh Kothari kothariyugesh at gmail.com
Wed Mar 27 03:54:40 UTC 2019


Extremely sorry,

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

---------- Forwarded message ---------
From: Yugesh Kothari <kothariyugesh at gmail.com>
Date: Wed, 27 Mar, 2019, 9:18 AM
Subject: Re: [Haskell-beginners] IO String and String using readFile
To:
Cc: <beginners at haskell.org>


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, <ian at zenhack.net> 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?
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.haskell.org/pipermail/beginners/attachments/20190327/670b9613/attachment-0001.html>


More information about the Beginners mailing list