[Haskell-cafe] IO Monad operation madness

Isaac Dupree isaacdupree at charter.net
Thu May 17 19:15:26 EDT 2007


Rob Hoelz wrote:
> The problem I have with that is that I'd have to do something like this:
> 
> str <- peekCString =<< (linked_list_getdata ptr)
> next <- linked_list_next ptr
> rest <- linkedListToHaskellStringList next
> return (str : rest)
Exactly! That's normal Haskell code!

> 
> I don't like this because it's not really tail recursive.  I've been
> told tail recursion is overrated, but since I don't really know how
> long the C linked lists are going to be (could be 10 elements, could be
> 100), I don't think avoiding a stack overflow is overrated.

If you're worried about stack overflows, make a million element list and
try your Haskell code on it (with optimizations if applicable).  See if
it actually stack overflows.  Hugs is relatively bad, GHC usually
doesn't stack overflow but it has happened to me (once).

refactoring:
> str <- peekCString =<< (linked_list_getdata ptr)
> rest <- linkedListToHaskellStringList =<< linked_list_next ptr
> return (str : rest)

further:
> liftM2 (:)
>   (peekCString =<< (linked_list_getdata ptr)
>   (linkedListToHaskellStringList =<< linked_list_next ptr)
(import Control.Monad if necessary for liftM2)

Okay, does that look weird enough for you?

Isaac



More information about the Haskell-Cafe mailing list