Recursion, Monad and Laziness

I am always excited about being able to write software that is easy to read and easy to maintain. Haskell is supposed to provide a lot of greatness and so I have been trying to enhance my knowledge of Haskell but more importantly all the concepts that comes with this beautiful language. I have a program that runs pretty well and I tried to do some refactoring (the more you learn the Haskell idioms, then more you hate your initial code :)). After refactoring, the code went into infinite loop and I was stumped. I was counting on the laziness and the functor features to keep the code terse.

The situation is that the original function wasn’t recursive (only 1 level) before refactoring. However, to reduce the complexity of if/else for the caller, I made it indirectly recursive (a -> b -> a) in one case. Further, I relied on the laziness so that it won’t be recursive forever without explicit base case handling. But when I had a pure function c that is lifted into the b which is an IO monad, I had the problem. By rewriting the c function itself work directly with monads solved the problem.

This entry was posted in Haskell. Bookmark the permalink.

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.