Newbie question on "statefullness"

Andrew J Bromage ajb@spamcop.net
Mon, 12 Aug 2002 11:27:44 +1000


G'day all.

On Sun, Aug 11, 2002 at 05:36:21PM -0700, Alex Peake wrote:

> I am new to Haskell. I want to do something very simple (I thought)
> but got lost in the world of Monads.
> 
> I want to implement something like the C idea of:
> n += i
> 
> So how does one doe this in Haskell?

I think this needs to be an FAQ.

The short answer is that if you find yourself needing to do this,
especially if you're new to Haskell, you're probably thinking about
the problem in the wrong way.  Haskell does not support the "n += i"
idiom in the same way that C does not support, say, higher-order
functions.

The flip side is that Haskell _does_ support the "n += i" idiom in
the same way that C _does_ support higher-order functions, in that
with some effort (sometimes a little, sometimes a lot) you can simulate
the same functionality if you find you really need it (using monads,
continuations or whatever).  However, most of the time where you
would use this idiom in C, you would not use it in the equivalent
Haskell program, simply because there's usually a more appropriate
way of phrasing your intentions.

Cheers,
Andrew Bromage