As i understand Haskell does not have a global state, so is there any way to write a function f that will return f(n – 1) + 1, where n is a number of function call and f(1) = 0.
It should not accept any arguments and used like func f
Prelude> f ()
0
Prelude> f ()
1
Without using tricks like
unsafePerform, it is not possible to define a function that can be called like you showed in your example. However it is possible to define an IO action that does what you want and could be used like this:Here’s an example program that does what you want using IORefs: