Search code examples
haskellstatemonads

where does haskell save state?


People say Haskell doesn't have state. I think that practical programs need state. The same goes for haskell. Haskell has no variable for saving state, how does haskell save state? I think that haskell use lambda variable in head as a MEMORY!

    someAction1 >>= \result1 ->
  ( someAction2 >>= \result2 ->
  ( someAction3 >>= \result3 -> return (somef result1 result2 result3)))

last function somef can get result of someAction1, someAction2, someAction3 through result1, result2, result3

lambda variables (result1, result2, result3) play a role like a MEMORY (variable for saving state).

"Haskell doesn't have a state" doesn't mean It doesn't need the state concept for practical program.

The reason why lambda algebra can do the same thing as turing complete is because of the scope of the lambda variable. Because lambda variables are used like a memory, general-purpose programming is possible.

did I get it right?


Solution

  • "People say Haskell doesn't have state."

    Haskell does have state, it's just that most of the time it's very transitory. Take for example the following function.

    mysum :: [Integer] -> Integer
    mysum [] = 0
    mysum (x:xs) = x + mysum xs
    

    This function is crawling with state, with values held on the stack. The only two long term values are the input list and the output value. If this function is called by another function then not even these values are long term.

    The do notation looks like it has state, but this is just syntactic sugar. The code which looks imperative is turned into a sequence of linked lambda expressions. Desugaring do-notation for Monads

    Where long-term state is required, it can be stored in a State Monad or a database.