Does Haskell have variables?

风流意气都作罢 提交于 2019-11-27 12:39:42
Don Stewart

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.

The simple answer is: yes, Haskell has variables as defined in Section 3.2 of the Haskell Report. Variables can appear in patterns and can thus be bound to a value using constructs like let, case, and list comprehensions.

Perhaps implicit in your questions is whether a variable is properly called a variable if it is immutable. I think the other answers cover mutability sufficiently.

Yes, Haskell has variables. Consider the (essentially equivalent) definitions

inc n = n + 1
inc = \n -> n + 1

In both these cases, n is a variable; it will take on different values at different times. The Haskell Report, in Section 3 refers to these explicitly as variables.

That n here is a variable may be easier to see if we consider the following complete program:

inc n = n + 1
f = inc 0
g = inc 1
main = print (f+g)

The answer printed will be "3", of course. When evaluating f, as we expand inc x will take on the value 0, and when later (or earlier!) evaluating g, as we expand inc x will take on the value 1.

Some confusion may have arisen because Haskell, as with the other languages listed in the question, is a single-assignment language: it does not allow the reassignment of variables within a scope. Once n has been assigned the value 42, it cannot be anything but 42 without introducing a new scope with a new n (which is a different variable, shadowing the other n) bound to another value.

This may not be entirely obvious in some contexts, such as expressions using do:

 do let n = 1
    print n
    let n = 2
    print n

but if you remove the syntactic sugar, translating it into Haskell without the do, it becomes clear that there was a new, nested scope created where the n in that inner scope is a different variable that is shadowing the n in the outer scope:

(let n = 1 
  in (print n >> (let n = 2
                   in print n)))

"I have heard that Haskell doesn't have variables. Is this true?"

No.

"So does it have variables or not, and why?"

Yes.

EDIT: My answer causes a double-negative, which is naturally confusing because the headline question is positive while the body isn't. :)

EDIT2: Edited again, since the OP changed the question.

According to [Wikipedia](http://en.wikipedia.org/wiki/Variable_(programming)), yes, Haskell has variables:

In computer programming, a variable is an identifier (usually a letter or word) that is linked to a value stored in the system's memory or an expression that can be evaluated. For instance, a variable might be called "total_count" and contain a number.
In imperative programming languages, values can generally be accessed or changed at any time. However, in pure functional and logic languages, variables are bound to expressions and keep a single value during their entire lifetime due to the requirements of referential transparency. In imperative languages, the same behavior is exhibited by constants, which are typically contrasted with normal variables.

Not that all Wikipedia definitions are perfectly trustworthy, of course.

The page on [mathematical variables](http://en.wikipedia.org/wiki/Variable_(mathematics)) may provide further insight into this.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!