Is Erlang “single assignment” different from Haskell “immutable values”?

后端 未结 5 1068
失恋的感觉
失恋的感觉 2020-12-15 07:51

In the \"Programming Erlang\" book it\'s said that the language uses \"single assignment\" variables. In other articles about functional programming languages I always read

5条回答
  •  误落风尘
    2020-12-15 08:14

    Haskell programmer reporting in. Yes, "single assignment" and "immutable values" are both the exact same thing. Haskell takes this concept a bit deeper: everything's value is defined at compile time. Yes, everything.

    You might then ask how that is possible, since Haskell can clearly do I/O. The answer is that you when you define an operation that extracts an input from the outside world, you are not defining the value, but merely the operation that extracts it. You never explicitly define the value that is bound. In Haskell, when you do things like:

    echo = forever $ do
        x <- getLine
        putStrLn x
    

    You are not "defining" x but rather just just telling getLine and putStrLn how to interact. The only thing you are actually defining is echo, which is nothing more than an action waiting to be run. Clearly, echo's behavior is defined at compile time, whereas the value of x is not.

    Maybe you already knew that, but I'm writing this also for the benefit of other people who have a similar question about Haskell vs. Erlang.

提交回复
热议问题