Repeat an action indefinitely.
Using
ApplicativeDo: '
forever as' can be
understood as the pseudo-
do expression
do as
as
..
with
as repeating.
Examples
A common use of
forever is to process input from network
sockets,
Handles, and channels (e.g.
MVar and
Chan).
For example, here is how we might implement an
echo server,
using
forever both to listen for client connections on a
network socket and to echo client input on client connection handles:
echoServer :: Socket -> IO ()
echoServer socket = forever $ do
client <- accept socket
forkFinally (echo client) (\_ -> hClose client)
where
echo :: Handle -> IO ()
echo client = forever $
hGetLine client >>= hPutStrLn client