[Haskell-cafe] Simple but interesting (for me) problem

Brent Yorgey byorgey at seas.upenn.edu
Wed Oct 21 13:47:55 EDT 2009


On Wed, Oct 21, 2009 at 10:34:47AM -0700, michael rice wrote:
> There's a thread on the plt-scheme list about creating a function of NO arguments named NEXT that just returns the number of times it's been called, a piece of cake in Scheme, but how would one do this in Haskell? Would the best approach be to use a State monad?

Yes, a State monad would be the way to go.  Such a function cannot
have type () -> Int in Haskell, because a function with type () -> Int
must be pure: in particular it would always have to give the same
output.  So giving the function the type () -> State Int Int (or
really, just State Int Int) makes explicit the fact that evaluating it
has a state effect.

-Brent


More information about the Haskell-Cafe mailing list