Does Haskell have variables?

38,713

Solution 1

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.

Solution 2

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.

Solution 3

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)))

Solution 4

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.

Solution 5

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

Share:
38,713
cjs
Author by

cjs

Curt J. Sampson, &lt;[email protected]&gt;. Developer/DevOps/agile coach/etc. I like agile development, functional programming (especially Haskell) and retrocomputing.

Updated on May 09, 2020

Comments

  • cjs
    cjs almost 4 years

    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.

    So does it have variables or not, and why?

    This question also appears to apply ML, F#, OCaml, Erlang, Oz, Lava, and all SSA intermediate languages.

  • Hans-Kristian Norum Eidesen
    Hans-Kristian Norum Eidesen almost 15 years
    There are no variables in Haskell! Variables may be reassigned. What you have in functions or let-bindings is nothing but an immutable function argument, a value that is bound to a name!
  • cjs
    cjs almost 15 years
    Dario, wikipedia disagrees, listing nine languages where all variables are single-assignment and another five where single-assignment is an option: en.wikipedia.org/wiki/Single_assignment . Additionally, mathematicians, who came up with the term, also use variables in the single-assignment sense. If you're going to argue against this, how about posting a detailed answer showing why this is an incorrect view?
  • Dave
    Dave almost 15 years
    Could you quote the question you answered? Now when I open this question titled "Does Haskell have variables?" the first answer I see is "No". (Obviously, I didn't actually read the full question, just the title.)