Hi Cafe. I wonder if it is possible to write a IO putback function with following interface putback :: a -> IO a -> IO a putback x io = ??? where io is some action like reading from file or socket. I want putback to build new action which will return x on first call, and continue executing io after that. Thanks in advance! Sergey.
I'm sorry, but I don't fully understand what do you want from putback. If putback'ed action does io and then returns x, it's trivial: putback x io = io >> return x If you want putback'ed action to return x on its first call and do io on second, third... calls, then you need to put somewhere a mark indicating that action was called. You can use IORefs. import Data.IORef import System.IO.Unsafe putback x io = do m <- readIORef r if m then io else (writeIORef r True >> return x) where r = unsafePerformIO $ newIORef False Sergey Mironov <ierton@gmail.com> писал(а) в своём письме Sun, 15 May 2011 17:33:51 +0300:
Hi Cafe. I wonder if it is possible to write a IO putback function with following interface
putback :: a -> IO a -> IO a putback x io = ???
where io is some action like reading from file or socket. I want putback to build new action which will return x on first call, and continue executing io after that.
Thanks in advance! Sergey.
I think you need to change the type of putback slightly: import Data.IORef putback :: a -> IO a -> IO (IO a) putback a action = do next <- newIORef a return (do r <- readIORef next; writeIORef next =<< action; return r) main = do getChar' <- putback 'a' getChar str <- sequence $ take 10 $ repeat getChar' putStrLn str Thanks, Daniel On May 15, 2011, at 4:33 PM, Sergey Mironov wrote:
Hi Cafe. I wonder if it is possible to write a IO putback function with following interface
putback :: a -> IO a -> IO a putback x io = ???
where io is some action like reading from file or socket. I want putback to build new action which will return x on first call, and continue executing io after that.
Thanks in advance! Sergey.
_______________________________________________ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe
Thanks for your answers! I looked further and found that 'Oleg alredy did it'. Really, looks like iteratees will suit my needs :) I'm sorry for bothering. Sergey 2011/5/15 Daniel Gorín <dgorin@dc.uba.ar>:
I think you need to change the type of putback slightly:
import Data.IORef
putback :: a -> IO a -> IO (IO a) putback a action = do next <- newIORef a return (do r <- readIORef next; writeIORef next =<< action; return r)
main = do getChar' <- putback 'a' getChar str <- sequence $ take 10 $ repeat getChar' putStrLn str
Thanks, Daniel
On May 15, 2011, at 4:33 PM, Sergey Mironov wrote:
Hi Cafe. I wonder if it is possible to write a IO putback function with following interface
putback :: a -> IO a -> IO a putback x io = ???
where io is some action like reading from file or socket. I want putback to build new action which will return x on first call, and continue executing io after that.
Thanks in advance! Sergey.
_______________________________________________ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe
participants (3)
-
Artyom Kazak -
Daniel Gorín -
Sergey Mironov