5

I believe there are languages where all names with associated values cannot have their associated values changed. An example would be a language where all names behave like a does in the following C code:

int main(void)
{
  const int a = 0;
  a = 1;
}

i.e., all names act like what C calls a "read-only variable" here.

I know that certain constructs, like C's for loop, wouldn't be possible if one could not reassign variables. But I believe that languages where variables are exclusively read-only will use tail-recursive functions to accomplish looping.

If a language does not permit assigning new values to variables, what features (like tail recursion, in the above example) are needed to allow for programs to be written, that would otherwise be difficult / impossible to write without reassignment?

10
  • Can you give an example of such a language?
    – TZHX
    Commented Sep 18, 2016 at 18:38
  • 11
    Are you maybe asking about immutability in the context of functional programming?
    – MetaFight
    Commented Sep 18, 2016 at 18:52
  • @MetaFight Assuming that my "for instance" example were true (I don't know), I wondered, "More generally, does a 'constants only' rule impose other requirements on language features?" (So, I don't think it just has to do with functionalism.) Commented Sep 18, 2016 at 19:41
  • 2
    XSLT is a language of that type where all variables are immutable. See this SO post for an example how to write a loop without recursion.
    – Doc Brown
    Commented Sep 19, 2016 at 6:22
  • 1
    Are these constants only assigned values during compile time?
    – JeffO
    Commented Sep 19, 2016 at 17:09

1 Answer 1

7

With only constants, and without any variables, all your programs would be totally predictable and -- through constant propagation -- could be simply rewritten producing directly the expected output.

How would you manage user input in such a world ? How would you define even simple arithmetic functions, like f(x)=x^2 without precalculating and embedding in your program an infinity of combinations ?

However, there are several functional languages around. In these you have constants as in any language. But also variables/parameters, which can take a value (e.g. function parameter, user input), but can't be changed thereafter. We then don't talk about constants but about immutable variables.

In this functional approach you can write programs having the full expressiveness of a turing machine. The code is easier to analyze in terms of value propagation, as each function is stateless (i.e. it depends only on the function's input). And you don't have to cope with side effects on global or local variables. Keep in mind however that such functional language (e.g. Caml, or F#) are not yet mainstream.

8
  • 3
    It's not unheard of for functional programmers to refer to immutable variables as constants, in fact, in my reading, it's fairly common.
    – RubberDuck
    Commented Sep 18, 2016 at 20:43
  • 1
    @RubberDuck yes. And they are not the only ones, as some mainstream languages use a const qualifier for variables passed by reference to express that they should not be muted
    – Christophe
    Commented Sep 18, 2016 at 21:07
  • Thanks for pointing that out @RubberDuck. The "constants" I was referring to in my question actually were "immutable variables." Commented Sep 18, 2016 at 21:26
  • I'm not sure this is strictly true. Surely I can write a recursive chaotic function using only constants which will unpredicably halt say when it runs out of memory
    – Ewan
    Commented Sep 19, 2016 at 18:43
  • 2
    @Jackson functional programming is based on lambda calculus and it was proven that lambda calculus is Turing complete: cstheory.stackexchange.com/questions/625/… . To show it intuitively: instead of loops using counters an reusing variables, you'd use recursive functions only binding new values to parameter instantiations. Every step is a function applied to the result of the previous steps, and so on until the first step
    – Christophe
    Commented Sep 20, 2016 at 5:55

Your Answer

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

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