36

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.

2
  • This question doesn't really apply to SSA. SSA doesn't have mutable variables for a different reason than that of Haskell. Haskell does not have mutable variables to honor referential transparency. SSA doesn't have mutable variables to aid analysis.
    – xilpex
    Aug 29, 2020 at 22:47
  • @xilpex Your comment begs the question by assuming that both SSA and Haskell have "variables." Note that my question is not about why languages have immutable variables_, but whether those can be considered variables at all.
    – cjs
    Sep 2, 2020 at 2:28

5 Answers 5

58

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.

10

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.

9

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)))
2
  • 2
    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!
    – Dario
    Jun 14, 2009 at 16:56
  • 12
    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?
    – cjs
    Jun 14, 2009 at 17:09
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.

0
2

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

1
  • 1
    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.) Jun 14, 2009 at 18:11

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.