[Haskell-cafe] Re: Tail Recursion within the IO Monad

Simon Marlow simonmarhaskell at gmail.com
Thu May 17 06:22:34 EDT 2007


Rob Hoelz wrote:
> "Brandon S. Allbery KF8NH" <allbery at ece.cmu.edu> wrote:
> 
>> On May 16, 2007, at 12:23 , Rob Hoelz wrote:
>>
>>> And as long as I'm asking, is there some kind of monadic function
>>> composition operator?  I'd like to clean up the above with something
>>> like peekCString . peek . linked_list_getdata...
>> (=<<)?
>>
> 
> Thanks for the reply;  I can't believe I missed that one!  But while
> looking over the documentation, completely humbled, I discovered
> sequence, which allows me to write my code cleanly!  Thanks for the
> help!

sequence still isn't tail-recursive, although sequence_ is.  If you want a 
tail-recursive sequence, the only way to do it is like this:

sequence' :: [IO a] -> IO [a]
sequence' ms = do
   let as = map unsafePerformIO ms
   foldr seq (return ()) as
   return as

although that's likely to be a lot slower, too.

Cheers,
	Simon


More information about the Haskell-Cafe mailing list