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.
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.
For larger programs, it is better to hide the state passing plumbing in the monad. There is less risk of error then.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With