Does Haskell have variables?

前端 未结 5 1066
鱼传尺愫
鱼传尺愫 2020-12-03 01:05

I\'ve frequently heard claims that Haskell doesn\'t have variables; in particular, this answer claims that it doesn\'t, and it was upvoted at least nine times and accepted.<

5条回答
  •  独厮守ぢ
    2020-12-03 01:29

    Haskell has immutable variables (variables in the math sense) by default:

     foo x y = x + y * 2
    

    By default variables are not mutable cells.

    Haskell also has mutable cells though, but you enable them explicitly:

     > import Data.IORef (newIORef, readIORef, writeIORef)
     > v <- newIORef 0
     > readIORef v
     0
    
     > writeIORef v 7
     > readIORef v
     7
    

    So, YES Haskell has true variables. But it does not use mutable variables by default.

提交回复
热议问题