Deep confusion about monads

Adrian Hey ahey@iee.org
Fri, 17 Aug 2001 02:36:28 +0100


Hello again,

On Thursday 16 August 2001  9:39 pm, Mark Carroll wrote:
> That's embarrassing - I hit 'send' before I finished. Sorry. ):
>
> On Thu, 16 Aug 2001, Mark Carroll wrote:
> > I should add that I do seem to have written something that sort of works:
> >
> > firstChar :: [a] -> IO a
> > firstChar xs = return head xs
>
> I mean firstChar xs = return $ head xs
>
> > then,
> >
> > readFile "/tmp/foo" >>= firstChar >>= putChar
>
> (snip)
>
> ...what's surprising me is that do I really have to turn everything into
> an IO action like this just to do things with the String hidden in the IO
> String?
>

No, you could write instead..
 (readFile "/tmp/foo") >>= (\foofile -> putChar (head foofile))
or..
 (readFile "/tmp/foo") >>= (putChar . head)

HTH

Regards
-- 
Adrian Hey