Send Beginners mailing list submissions to
[email protected]
To subscribe or unsubscribe via the World Wide Web, visit
http://www.haskell.org/mailman/listinfo/beginners
or, via email, send a message with subject or body 'help' to
[email protected]
You can reach the person managing the list at
[email protected]
When replying, please edit your Subject line so it is more specific
than "Re: Contents of Beginners digest..."
Today's Topics:
1. IO vars (Corentin Dupont)
2. Re: IO vars (Mihai Maruseac)
3. Re: IO vars (Corentin Dupont)
4. Re: IO vars (Eugene Perederey)
5. Re: IO vars (Ozgur Akgun)
----------------------------------------------------------------------
Message: 1
Date: Wed, 29 Aug 2012 10:58:58 +0200
From: Corentin Dupont <[email protected]>
Subject: [Haskell-beginners] IO vars
To: [email protected]
Message-ID:
<caeyhvmo7hw5gq3m5yzovadjex7q1pny05d12gtw8mr66a2b...@mail.gmail.com>
Content-Type: text/plain; charset="iso-8859-1"
Hi all,
there is something very basic that it seems escaped me.
For example with the following program f and g have type IO () and I can
thread a value between the two using a file.
Can I do the exact same (not changing the types of f and g) without a file?*
f,g :: IO ()
**f = withFile "toto" WriteMode (flip hPutStr "toto")
g = withFile "toto" ReadMode hGetLine >>= putStrLn
main = f >> g*
Thanks and cheers,
Corentin
-------------- next part --------------
An HTML attachment was scrubbed...
URL:
<http://www.haskell.org/pipermail/beginners/attachments/20120829/43c5919f/attachment-0001.htm>
------------------------------
Message: 2
Date: Wed, 29 Aug 2012 11:04:06 +0200
From: Mihai Maruseac <[email protected]>
Subject: Re: [Haskell-beginners] IO vars
To: Corentin Dupont <[email protected]>
Cc: [email protected]
Message-ID:
<CAOMsUMJ4cNbK4tG5Afxg41KKcGwdTXudKRrd+Tf=p0yspfk...@mail.gmail.com>
Content-Type: text/plain; charset=ISO-8859-1
On Wed, Aug 29, 2012 at 10:58 AM, Corentin Dupont
<[email protected]> wrote:
> Hi all,
> there is something very basic that it seems escaped me.
> For example with the following program f and g have type IO () and I can
> thread a value between the two using a file.
> Can I do the exact same (not changing the types of f and g) without a file?
>
> f,g :: IO ()
> f = withFile "toto" WriteMode (flip hPutStr "toto")
> g = withFile "toto" ReadMode hGetLine >>= putStrLn
> main = f >> g
Of course:
f,g :: IO ()
f = putStr "Answer: "
g = print 42
Main> f >> g
Answer: 42
The () is threaded by >>, not the file content
--
MM
------------------------------
Message: 3
Date: Wed, 29 Aug 2012 11:21:06 +0200
From: Corentin Dupont <[email protected]>
Subject: Re: [Haskell-beginners] IO vars
To: Mihai Maruseac <[email protected]>
Cc: [email protected]
Message-ID:
<CAEyhvmqxMMqKJDh8aqmLUc6+wXj2GgeWTbfw=in833bh9qu...@mail.gmail.com>
Content-Type: text/plain; charset="iso-8859-1"
Hi Mihai,
maybe the term "thread" in my mail is not correct.
What I mean is that a value gets stored by f and discovered by g.
*f,g :: IO ()
f = withFile "toto" WriteMode (flip hPutStr "42")
g = withFile "toto" ReadMode hGetLine >>= (\s -> putStrLn $ "Answer:" ++ s)
main = f >> g*
Is it possible to do the same without files (the types must remain IO())?
On Wed, Aug 29, 2012 at 11:04 AM, Mihai Maruseac
<[email protected]>wrote:
> On Wed, Aug 29, 2012 at 10:58 AM, Corentin Dupont
> <[email protected]> wrote:
> > Hi all,
> > there is something very basic that it seems escaped me.
> > For example with the following program f and g have type IO () and I can
> > thread a value between the two using a file.
> > Can I do the exact same (not changing the types of f and g) without a
> file?
> >
> > f,g :: IO ()
> > f = withFile "toto" WriteMode (flip hPutStr "toto")
> > g = withFile "toto" ReadMode hGetLine >>= putStrLn
> > main = f >> g
>
> Of course:
>
> f,g :: IO ()
> f = putStr "Answer: "
> g = print 42
>
> Main> f >> g
> Answer: 42
>
> The () is threaded by >>, not the file content
>
> --
> MM
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL:
<http://www.haskell.org/pipermail/beginners/attachments/20120829/e386960c/attachment-0001.htm>
------------------------------
Message: 4
Date: Wed, 29 Aug 2012 02:28:45 -0700
From: Eugene Perederey <[email protected]>
Subject: Re: [Haskell-beginners] IO vars
To: Corentin Dupont <[email protected]>
Cc: [email protected]
Message-ID:
<CAFTqo156WjDWQ1BjrtCJdJQJwsb3PNw=ivzxc3-=m0zan35...@mail.gmail.com>
Content-Type: text/plain; charset=ISO-8859-1
Of course, not.
What you do with a file is a sequence of side effects done by f, then g.
If you want to reuse the value of type IO a returned by f, your g
function would need to have type g:: a->IO ()
so that you combine the actions: f >>= g
On 29 August 2012 02:21, Corentin Dupont <[email protected]> wrote:
> Hi Mihai,
> maybe the term "thread" in my mail is not correct.
> What I mean is that a value gets stored by f and discovered by g.
>
> f,g :: IO ()
> f = withFile "toto" WriteMode (flip hPutStr "42")
> g = withFile "toto" ReadMode hGetLine >>= (\s -> putStrLn $ "Answer:" ++ s)
> main = f >> g
>
> Is it possible to do the same without files (the types must remain IO())?
>
>
>
> On Wed, Aug 29, 2012 at 11:04 AM, Mihai Maruseac <[email protected]>
> wrote:
>>
>> On Wed, Aug 29, 2012 at 10:58 AM, Corentin Dupont
>> <[email protected]> wrote:
>> > Hi all,
>> > there is something very basic that it seems escaped me.
>> > For example with the following program f and g have type IO () and I can
>> > thread a value between the two using a file.
>> > Can I do the exact same (not changing the types of f and g) without a
>> > file?
>> >
>> > f,g :: IO ()
>> > f = withFile "toto" WriteMode (flip hPutStr "toto")
>> > g = withFile "toto" ReadMode hGetLine >>= putStrLn
>> > main = f >> g
>>
>> Of course:
>>
>> f,g :: IO ()
>> f = putStr "Answer: "
>> g = print 42
>>
>> Main> f >> g
>> Answer: 42
>>
>> The () is threaded by >>, not the file content
>>
>> --
>> MM
>
>
>
> _______________________________________________
> Beginners mailing list
> [email protected]
> http://www.haskell.org/mailman/listinfo/beginners
>
--
Best,
Eugene Perederey
------------------------------
Message: 5
Date: Wed, 29 Aug 2012 10:43:22 +0100
From: Ozgur Akgun <[email protected]>
Subject: Re: [Haskell-beginners] IO vars
To: Corentin Dupont <[email protected]>
Cc: [email protected]
Message-ID:
<CALzazPCSOzyTzR7SwZ45z8E=nzO3YA43S=VR0tNFk2=cgjn...@mail.gmail.com>
Content-Type: text/plain; charset="utf-8"
On 29 August 2012 10:21, Corentin Dupont <[email protected]> wrote:
> *f,g :: IO ()
> f = withFile "toto" WriteMode (flip hPutStr "42")
> g = withFile "toto" ReadMode hGetLine >>= (\s -> putStrLn $ "Answer:" ++ s)
> main = f >> g*
>
> Is it possible to do the same without files (the types must remain IO())?
>
One can use an IORef to get a similar effect.
import Data.IORef
import System.IO.Unsafe
{-# NOINLINE toto #-}
toto :: IORef String
toto = unsafePerformIO (newIORef "")
f,g :: IO ()
f = writeIORef toto "42"
g = readIORef toto >>= (\s -> putStrLn $ "Answer:" ++ s)
main = f >> g
HTH,
Ozgur
-------------- next part --------------
An HTML attachment was scrubbed...
URL:
<http://www.haskell.org/pipermail/beginners/attachments/20120829/b705a71c/attachment-0001.htm>
------------------------------
_______________________________________________
Beginners mailing list
[email protected]
http://www.haskell.org/mailman/listinfo/beginners
End of Beginners Digest, Vol 50, Issue 36
*****************************************