I\'ve been reading a lot of stuff about functional programming lately, and I can understand most of it, but the one thing I just can\'t wrap my head around is stateless codi
I think there's a slight misunderstanding. Pure functional programs have state. The difference is how that state is modeled. In pure functional programming, state is manipulated by functions that take some state and return the next state. Sequencing through states is then achieved by passing the state through a sequence of pure functions.
Even global mutable state can be modeled this way. In Haskell, for example, a program is a function from a World to a World. That is, you pass in the entire universe, and the program returns a new universe. In practise, though, you only need to pass in the parts of the universe in which your program is actually interested. And programs actually return a sequence of actions that serve as instructions for the operating environment in which the program runs.
You wanted to see this explained in terms of imperative programming. OK, let's look at some really simple imperative programming in a functional language.
Consider this code:
int x = 1;
int y = x + 1;
x = x + y;
return x;
Pretty bog-standard imperative code. Doesn't do anything interesting, but that's OK for illustration. I think you will agree that there's state involved here. The value of the x variable changes over time. Now, let's change the notation slightly by inventing a new syntax:
let x = 1 in
let y = x + 1 in
let z = x + y in z
Put parentheses to make it clearer what this means:
let x = 1 in (let y = x + 1 in (let z = x + y in (z)))
So you see, state is modeled by a sequence of pure expressions that bind the free variables of the following expressions.
You will find that this pattern can model any kind of state, even IO.