
Occasionally I have a non-monadic function (call it "x") that takes a function (call it "y") as an argument and uses it for stuff. Then, one day I inevitably want to pass it a y that needs to do something monadically: e.g. when I wanted unfoldM instead of unfoldr. So, I have to rewrite x accordingly. If I want to write an x that is open to taking a monadic y but doesn't require it to be monadic, should I maybe write it monadically and provide a wrapper that puts non-monadic y's in the Identity monad? Might this wrapping approach cause less efficient code than writing an alternative non-monadic x for non-monadic y's? Am I making any sense at all? (-: -- Mark

I could give an example, actually, which might make things clearer. Take one of my unfoldM's: unfoldM f base = do fb <- f base case fb of Nothing -> return [] Just (a, b) -> do rest <- unfoldM f b return (a : rest) Now, using the Identity monad I can write a wrapper for non-monadic f's: myUnfoldr f b = runIdentity (unfoldM (return . f) b) which works the same as a regular unfoldr. With a monadic function and a wrapper that uses the Identity monad, I can offer both monadic and non-monadic interfaces to the unfolding functionality that is only implemented once. Is this a good design idea? Are there better ones? Is myUnfoldr necessarily less efficient than a "normal" unfoldr that doesn't have monads underneath constraining evaluation order? -- Mark

Mark Carroll writes:
[a definition of unfold in terms of monadic unfold in the identity monad]
Is this a good design idea? Are there better ones? Is myUnfoldr necessarily less efficient than a "normal" unfoldr that doesn't have monads underneath constraining evaluation order?
Actually, though the operations of most monads constrain evaluation order, an interesting property of the identity monad is that it should not do so. This observation has led me to think long and hard about the distinction between monadic and non-monadic computations in Haskell (admittedly to no real conclusion). -Jan-Willem Maessen jmaessen@alum.mit.edu
participants (3)
-
Jan-Willem Maessen
-
Mark Carroll
-
Mark T.B. Carroll