Question: if I want to express something like [just a simple example]:

```
int a = 0;
for (int x = 0; x < n; x += 1)
    a = 1 - a;

```

what should I do in Haskell, *since it doesn't have variable concept?* (maybe wrong, see: [Does Haskell have variables?](https://stackoverflow.com/questions/993124/does-haskell-have-variables))

Answer: 
There are a few options. First, you can rewrite the problem with naive recursion:

```
loop :: Int -> Int
loop n = loop' n 0
  where loop' 0 a = a
        loop' n a = loop' (n - 1) (1 - a)

```

Next, you can restate recursion as a fold:

```
loop :: Int -> Int
loop n = foldr (\a _ -> 1 - a) 0 [0..n]

```

Or you can use `State` to simulate a for loop:

```
import Control.Monad
import Control.Monad.State

loop :: Int -> Int
loop n = execState (forM_ [0..n] 
                      (\_ -> modify (\a -> 1 - a))) 0

```
Another option:

```
iterate (\a -> 1-a) 0 !! n
-- or even
iterate (1-) 0 !! n

```

The snippet `iterate (\a -> 1-a) 0` produces an infinite lazy list of all the values obtained starting from `0` and repeatedly applying the function `(\a -> 1-a)`. Then `!! n` takes the n-th element.

To be completely honest, in this case, I'd also look for a stricter definition of `iterate` which does not create so many lazy thunks.