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: 
Often, repetition that you would perform with a loop in a procedural language is accomplished with [recursion](http://learnyouahaskell.com/recursion) in Haskell. In this case, you should think about what the result of the loop is. It appears to alternate between 0 and 1. There are several ways to do this in Haskell. One way is

```
alternatingList n = take n alternating0and1
alternating0and1 = 0 : alternating1and0
alternating1and0 = 1 : alternating0and1

```
The other answers have already explained how you would approach a problem like this functionally, in Haskell.

However, Haskell does have mutable variables (or references) in the form of [ST](https://hackage.haskell.org/package/base-4.9.0.0/docs/Control-Monad-ST.html) actions and [STRef](https://hackage.haskell.org/package/base-4.9.0.0/docs/Data-STRef.html). Using them is usually not very pretty, but it does allow you to express imperative, variable-mutating code faithfully in Haskell, if you really want to.

Just for fun, here's how you might use them to express your example problem.

(The following code also uses [whileM\_](http://hackage.haskell.org/package/monad-loops-0.4.3/docs/Control-Monad-Loops.html#v:whileM_) from the [monad-loops](http://hackage.haskell.org/package/monad-loops) package, for convenience.)

```
import Control.Monad.Loops
import Control.Monad.ST
import Data.STRef

-- First, we define some infix operators on STRefs,
-- to make the following code less verbose.

-- Assignment to refs.
r @= x = writeSTRef r =<< x
r += n = r @= ((n +) <$> readSTRef r)

-- Binary operators on refs. (Mnemonic: the ? is on the side of the ref.)
n -? r = (-) <$> pure n <*> readSTRef r
r ?< n = (<) <$> readSTRef r <*> pure n

-- Armed with these, we can transliterate the original example to Haskell.
-- This declares refs a and x, mutates them, and returns the final value of a.
foo n = do
    a <- newSTRef 0
    x <- newSTRef 0
    whileM_ (x ?< n) $ do
        x += 1
        a @= (1 -? a)
    readSTRef a

-- To run it:
main = print =<< stToIO (foo 10)

```