[Haskell-beginners] More direct binding of IO result
Daniel Fischer
daniel.is.fischer at googlemail.com
Sun May 15 12:37:56 CEST 2011
On Sunday 15 May 2011 08:29:10, Michael Snoyman wrote:
> On Sun, May 15, 2011 at 9:27 AM, Christopher Howard <
>
> christopher.howard at frigidcode.com> wrote:
> > I understand that one can bind the unwrapped results of IO functions
> > to variables, and pass them to functions, like so:
> >
> > main = do filecontents <- readFile "data.txt"
> >
> > putStrLn filecontents
> >
> > But does the syntax allow you to cut out the middle man, so to speak,
> > and bind the results directly to the parameter? Like
> >
> > -- Tried this and it didn't work.
> > main = do putStrLn (<- readFile "data.txt")
> >
> > do-notation is just syntactic sugar for the ">>" and ">>=" operators.
> > Your
>
> example gets translated internally to:
>
> readFile "data.txt" >>= \filecontents -> putStrLn filecontents
>
> To get rid of that extra "filecontents", you could instead write:
>
> readFile "data.txt" >>= putStrLn
Or, if you prefer to read your data-flow right-to-left:
putStrLn =<< readFile "data.txt"
More information about the Beginners
mailing list