3

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?

  • 9
    I think you are confusing "state" with "mutable state". – n. m. could be an AI Jan 30 '22 at 08:19
  • It means specific scope variables such as global variables. – Lionhairdino L. Jan 30 '22 at 08:26
  • 6
    If by "state" you mean variables, then yes, Haskell has variables, including scoped and global variables. No one claims it doesn't. They are just not mutable. They are defined, rather than assigned. People that say "Haskell doesn't have state" mean *mutable* state. Actually Haskell has mutable state, it just needs to be accessed through a monadic interface of some kind. But "ordinary" non-monadic computations do not have mutable state. – n. m. could be an AI Jan 30 '22 at 08:35
  • 2
    thanks for your comment. You are right. My question is wrong.. I mean.. For example, parsec combinators do not combine parser and parser. (a -> parser a) form is combined. The reason is that "a" is used as a memory that stores parsing results for each stage. I mean this result as a state. – Lionhairdino L. Jan 30 '22 at 09:30
  • 4
    One could consider [SKI combinator calculus](https://en.wikipedia.org/wiki/SKI_combinator_calculus) to be a model of computation without variables. So variables are probably not as mandatory for computing as you think. The [Unlambda Programming Language](http://www.madore.org/~david/programs/unlambda/) has example code to compute Fibonacci numbers. – Micha Wiedenmann Jan 30 '22 at 09:50
  • Related (and specifically about actual mutable state of the kind n. 1.8e9-where's-my-share m. alludes to): [*Updating an outer variable in Haskell*](https://stackoverflow.com/q/70817534/2751851) – duplode Jan 30 '22 at 14:36
  • 1
    see if "how to think functional" section in [this answer](https://stackoverflow.com/questions/11923567/how-to-increment-a-variable-in-functional-programming/849891) clarifies things. in short yes, you are correct, arguments are used to pass changing values, used as state. this is known as state passing. – Will Ness Feb 01 '22 at 19:25

1 Answers1

4

"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.

Francis King
  • 1,652
  • 1
  • 7
  • 14