views:

265

answers:

5

Are there programming languages whose "variables" are immutable (const, final, etc) by default?

And, to make it variable, you need to declare an additional immutable qualifier ?

+18  A: 

The philosophy of the Functional Programming paradigm is strongly geared towards all "variables" being immutable, and "mutable" ones being only allowed in extreme cases (ie, for I/O). Therefore, most functional programming languages like the various Lisp dialects and Haskell will (more often than not) reinforce immutable variables. Of course, some languages offer more flexibility than others, but the central paradigm/philosophy remains, that discourages it.


Edit: In response to your edit:

If you are looking for a C-with-friendlier-constants, then adopting an entirely new programming paradigm isn't your solution. In Functional programming, everything is immutable (in most cases), and you're generally looking at a whole new fundamental philosophy and approach to programming.

This isn't really a solution if all you want is to make it easier to declare constants.


Edit2: In response to people upvoting me:

While I did correctly (I hope) answer the asker's question, I'm not sure it was an answer that turned out to be useful, given his new edit/comment. However, I can hope to possibly open his eyes to a whole new beautiful world of programming =)


Edit3: Here is Wikipedia's List of functional programming languages:

  • APL
  • Charity (purely functional)
  • Clean (purely functional)
  • Curl
  • Curry
  • Erlang
  • F#
  • Haskell (purely functional)
    • CAL
  • Hop
  • J
  • Joy
  • Kite
  • Lisp
    • Clojure
    • Common Lisp
    • Dylan
    • Little b
    • Logo
    • Scheme
    • Tea
  • Lush
  • Mathematica
  • Miranda
  • ML
    • Standard ML
    • Alice
    • Ocaml
    • Mythryl
  • Nemerle
  • Opal
  • OPS5
  • Poplog
  • R
  • Q
  • REFAL
  • Russell
  • Scala
  • Spreadsheets

Most of these languages have some minor elements/influences of non-functional heresy; the ones labeled "purely functional" do not.

(To my knowledge, Functional Programming languages are the only ones that encourage immutable variables by philosophy. There may be languages that have variables immutable, by default, that are not Functional by paradigm. The concept sounds quite odd to me, but I can't guarantee a blanket "never ever" statement, given the vast, vast, vast number of programming languages out there. I'll just say that it is, to my knowledge, extremely unlikely)

A commenter has suggested that ADA is an Imperative/Object-Oriented programming language with immutable variables by default.

Justin L.
Shut up and accept my upvote.
Hamish Grubijan
@Hamish, you sure have a sense of humor. Once I checked out your SO profile, I wasn't surprised. lol.
ShaChris23
I would weaken the comment regarding Lisp. *Some* Lisp dialects are immutable - most aren't.
Porges
Scala and Ada also require explicitly describing whether variables are mutable.
sarnold
What, no mention of F#? But +1 anyway. ;)
TrueWill
Added a list =) Also, clarified Lisp's loosiness as requested by Porges
Justin L.
Yeah, as a Common Lisp programmer, I'm not sure why it's considered a "functional language" by that WP list. It can be *used* for FP, but so can most languages. It's not like SML or F# or even Clojure.
Ken
I haven't programmed in Ada in 5 years, but I have no idea what the earlier comment is about. See examples of declaring Ada variables (and constants) here: http://www.seas.gwu.edu/~csci51/fall99/ada_note.html#variable_types
KeyserSoze
+4  A: 

Yes.

Haskell and Erlang are two examples.

Scott Wisniewski
+1  A: 

All variables in XSLT may only be immutable.

gef
+3  A: 

Clojure is a Lisp dialect targeting the Java Virtual Machine working exclusively with immutable data.

http://www.clojure.org

Mtgred
Clojure kicks F#'s ass!
Hamish Grubijan
-1 Clojure's philosophy is IMO to maximize immutable data, but use mutable reference when necessary. The problem with data mutation is that of safety and concurrency, but Clojure has STM that makes mutation of references safe.
ewernli
@ewerli> I think you misunderstood the concept of immutability in Clojure. Rich Hickey, the author of Clojure, explains in depth the concepts underlying the immutability in this keynote: http://www.infoq.com/presentations/Are-We-There-Yet-Rich-Hickey
Mtgred
+1  A: 

The closest analog I can think of is Objective Caml. Variables bound by let are always immutable, but the fields of structures (called records in Caml) are immutable only by default, and it is possible to tag an individual field as mutable.

Among functional languages, Objective Caml has proven remarkably attractive to C programmers. OCaml has quite a few imperative constructs in both the language and libraries, so the migration path is not so strange. And if you really want objects and a class system, they are there too.

Microsoft's .NET language F# is a descendant of Objective Caml, but there have been quite a few changes to make sure it is a .NET language, and I am not informed about mutability. But if you are wanting to try out a functional language as a relatively small change from your current programming style, you might wish to investigate Objective Caml or F#.

Norman Ramsey