Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it better to use the State monad, or to pass state recursively?

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.

like image 393
fadedbee Avatar asked Sep 21 '09 19:09

fadedbee


2 Answers

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.

like image 77
Macke Avatar answered Oct 02 '22 22:10

Macke


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

like image 44
Don Stewart Avatar answered Oct 02 '22 22:10

Don Stewart