views:

916

answers:

5

I've frequently heard claims that Haskell doesn't have varibles; 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.

I'd appreciate a comment explaining why you want to downvote or close the question, if you vote in this way. It seems to me that this is a basic computing science concept that's frequently misunderstood, and a question like this, with a good answer, can be a helpful reference to which to point people when they misunderstand it.

If you're going to downvote an answer, please supply your own answer giving details about why it's wrong, if you're downvoting it because it's incorrect. If you're downvoting it for other reasons, at least leave a comment.

A: 

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)))
Curt Sampson
There's nothing wrong with answering your own questions. But that doesn't mean it's great to post trivial questions.
Matthew Flaschen
You may also like to add this answer to the "favorite programmer-ignorance pet-peeve" question :)
Johannes Schaub - litb
I don't think it's trivial; see my first comment on my question above.
Curt Sampson
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
Dario, wikipedia disagrees, listing nine languages where all variables are single-assignment and another five where single-assignment is an option: http://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?
Curt Sampson
+3  A: 
Matthew Flaschen
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.)
Tom Lokhorst
+15  A: 

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:

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

Don Stewart
+4  A: 

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.

Chris Conway
+2  A: 

According to Wikipedia, 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 may provide further insight into this.

Curt Sampson
Wikipedia is the only infallable source of information on the interwebs.
trinithis