tags:

views:

252

answers:

2

I'm just learning Haskell, and trying to figure out the most idiomatic way to implement a line of sight algorithm.

The demo code I found uses the state monad, but it seem simpler to me (I'm just a beginner) to pass state recursively. What am I missing here? Are there performance problems?

Find code at: http://www.finalcog.com/bresenham-algorithm-idiomatic-haskell

Thanks,

Chris.

+7  A: 

It can become a bit verbose to pass state everywhere. Also, the state monad is well known by most haskell coders so they'll know what you're doing. If you hand-roll your own, outside a monad, it can be tricky to discern what your code does.

I find the state monad neat for encapsulating state changes, it's pretty obvious what part of your code is stateful (i.e. alters or depends on state) w.r.t. the rest of the pure stuff.

Marcus Lindblom
Learning how to use monads well is also important, so using them in a situation where you know how you would accomplish the same thing otherwise can be a good way to learn.
Amuck
+6  A: 

For larger programs, it is better to hide the state passing plumbing in the monad. There is less risk of error then.

Don Stewart