Writing a counter function

Mark Carroll mark@chaos.x-philes.com
Sun, 30 Jun 2002 10:32:31 -0400 (EDT)

On Sun, 30 Jun 2002, Jon Fairbairn wrote:
> But there's the rub. It's not beautiful and it doesn't make
> much sense. I really wish we could get away from the "How do
> I convert this imperative code snippet into Haskell"
> questions into "How do I solve this abstract problem?"

The question as originally posed didn't seem like it particularly needed
something imperative though. For instance, the Perl isn't strongly
imperative - it's largely just a list of declarations and functions (some
anonymous) where you can think of the variables as being locally-declared
constants. For instance, the first bit is very similar to, say,

	counter a = (a, \to_add -> counter (a + to_add))

I think that's very different from asking people to translate into Haskell
things where variables have their value change and whatever. Jon Cast's
observation makes more sense to me - it's not a imperative/functional
issue so much as a weak or strong typing issue.

> I guess that the last "$next" on the last line should have
> been "$next3", but I'm not certain, and I certainly have no
> idea what the programme is /for/.

Yes, I'm sure you're right there.

Thanks very much for sharing the monadic approach - I was curious as to if
monads could be used to break the recursion, and I didn't see anyone else
mention that. I've certainly found Jon Cast's, John Hughes' and Andrew
Bromage's articles interesting - it seems like this is a well-known issue
and Haskell currently lies on an attractive point on the tradeoff between
making things awkward and opening cans of worms.

-- Mark