Haskell is a pure functional language, that provides syntax sugar for the equivalent of imperative expressions. For example, this Python code:
def f():
n = 0
for i in range(10):
n += 1
return n
can be translated line by line into isomorphic Haskell code using monads.
Is the same true of code where a function mutates its surrounding environment? For example:
def f():
n = 0
def g():
nonlocal n
n += 1
for i in range(10):
g()
return n
Is there a way to translate the above line by line into isomorphic Haskell?
I'm perfectly prepared for the answer to be no, the code needs to be redesigned to do the same thing in a different way. But I figure it's worth asking if there is an isomorphic translation.