Skip to main content
32 votes
Accepted

Why do functional languages disallow reassignment of local variables?

In a pure functional programming language, there is no real notion of time at all. So, saying that a variable x has value a at one point and then b later simply ...
leftaroundabout's user avatar
11 votes

Why do functional languages disallow reassignment of local variables?

I can only share my perspective. The way I think of it is that mainstream functional languages typically combine two themes: (1) support for higher-order functions, and (2) a preference for pure ...
D.W.'s user avatar
  • 162k
10 votes
Accepted

The idea behind the state of Imperative languages

The simplest kind of state is the configuration of memory. In C this memory is accessed through variables (and arrays and pointers, but let us ignore those), so the state is the values of variables. ...
Andrej Bauer's user avatar
  • 30.9k
9 votes

Why do functional languages disallow reassignment of local variables?

If we want to be very precise with our words, we should say that in functional programming you don't assign a value to a variable, because assignment is a thing that you "do", and if you are ...
kaya3's user avatar
  • 520
8 votes
Accepted

For every imperative function, is there a functional counterpart with identical performance or even instructions?

Performance is not a property of language, it is a property of specific programs within a language. Some languages might be very fast at some things and slow at others. For example, Chez-Scheme can ...
Joey Eremondi's user avatar
7 votes

Why do functional languages disallow reassignment of local variables?

There are several reasons, but they boil down to three main points: It's easier to reason about correctness, especially in the context of complex semantics (e.g. existential types, lazy evaluation) ...
Pseudonym's user avatar
  • 22.3k
6 votes

Why do functional languages disallow reassignment of local variables?

In functional programming, great pains are taken to be able to know exactly what a computation depends on. The way in which you make variables be mutable, there can be cascading effects on the rest ...
Kyle Miller's user avatar
4 votes
Accepted

Can we add dependent type into an existing imperative programming language?

So, it's possible, but there are certainly some barriers: What kinds of expressions are allowed in types? What does it mean for a type to have a side-effect? Can typrchecking my program perform IO? ...
Joey Eremondi's user avatar
4 votes
Accepted

Why don't imperative languages like C or Go support Haskell-like parametric polymorphism?

The real answer is that the designers of those languages chose not to include it. It's certainly technically possible. As has been said in the comments, Rust does, Java does, C# does, etc. However, ...
Joey Eremondi's user avatar
4 votes
Accepted

Is purely functional programming in some situations less efficient than imperative programming?

Yes. There are problems where we can prove there is a $\Omega(\lg \lg n)$ factor slowdown. We also know that the slowdown is at most $O(\lg n)$ in all cases. See What classes of data structures can ...
D.W.'s user avatar
  • 162k
4 votes

Why do functional languages disallow reassignment of local variables?

What would it be like to have variable reassignment in functional programming? First i is 0, then you do something and change i to 1 ... But that is "do this, then that". Imperative ...
Odalrick's user avatar
  • 141
4 votes
Accepted

An imperative program is analogous to how a Turing machine works?

At a high enough level and when contrasted with functional programming, sure. Turing machine models and imperative programs have in common that they start from an input and take a series of steps that ...
usul's user avatar
  • 4,139
3 votes
Accepted

Plain-language example of how a functional style makes parallel programming easier

Functional programming is great for parallel programming that is not concurrent. The example problem you gave is inherently concurrent, but we can make it non-concurrent by "batching" the ...
Sam Westrick's user avatar
3 votes
Accepted

Is "imperative computing" just a fancy way of saying "scripting"? Or is there more to it?

Your understanding is not correct. There are two major programming paradigms: imperative programming and declarative programming. In imperative programming you basically tell your computer what to do ...
ttnick's user avatar
  • 1,658
3 votes

"Immediate" method of translating arbitrary mutable program to equivalent unmutable program?

The state monad allows us to translate any stateful (you call it "mutable") program to a pure one. The changes required to the program are "local" in the sense that you only need to make superficial ...
Andrej Bauer's user avatar
  • 30.9k
2 votes
Accepted

Best way to translate while loops to functions for software verification

I recommend that you translate while(c) b to def myWhile() = if (c) then (b ; myWhile()) myWhile() With this translation, ...
D.W.'s user avatar
  • 162k
2 votes
Accepted

Definition of "idempotence" of a function?

There are two related concepts called "idempotence" in programming. One is the mathematical one that quicksort and Raphael talk about. Namely, given a mathematical function $f$, $f$ is idempotent if $...
Derek Elkins left SE's user avatar
2 votes

Declarative, interrogative, imperative, and exclamative sentences in computer languages

The grammatical classification of the sentence types you're asking about isn't well-defined for programming languages because they refer to common ways humans respond to being talked to, which don't ...
Silver's user avatar
  • 417
2 votes

Why do functional languages disallow reassignment of local variables?

Optimizers in C++ turn traditional C++ code into static single assignment when it can. You can think of traditional imperative programming as chaining together functional steps. ...
Yakk's user avatar
  • 824
2 votes

Plain-language example of how a functional style makes parallel programming easier

I'm not sure functional programming does make parallel programming any easier, when taken in the round. If you program in a "functional style" (including all so-called good practices), then ...
Steve's user avatar
  • 562
2 votes

Why do functional languages disallow reassignment of local variables?

The answer is really purity. Pure functional languages strive to be pure functional languages. That involves not doing impure things. I'm just learning F# right now. I notice that it has a policy of ...
Cort Ammon's user avatar
  • 3,351
2 votes

Why do functional languages disallow reassignment of local variables?

Disallowing assignment is part of the definition of "functional programming" and "functional language". On one level, you're simply asking why the definition of "functional ...
Kaz's user avatar
  • 354
1 vote

Why do functional languages disallow reassignment of local variables?

numsum1 can be mechanically translated into numsum2. In fact, the translation is essentially the same as the one performed ...
benrg's user avatar
  • 2,157
1 vote

Is object oriented programming considered to be a declarative programming approach?

When you write an OO code, you describe object features (attributes, properties, methods) and implement them. This may be seen as declarative: you do not have an imperative approach giving the ...
Matthieu Latapy's user avatar
1 vote

Declarative, interrogative, imperative, and exclamative sentences in computer languages

I see the misconception. We have a name collision, where the same word is used to mean two different things in two different contexts. When Wikipedia says that SQL is a declarative programming ...
D.W.'s user avatar
  • 162k
1 vote
Accepted

Example on Referential transparency (wikipedia)

If Source refers to the keyboard, and the user types a the first time but b the second time, then GetInput(Source) will not return the same value both times. It's impossible to replace GetInput(Source)...
Gilles 'SO- stop being evil''s user avatar
1 vote

Is purely functional programming in some situations less efficient than imperative programming?

Another answer is that imperative (updatable) data structures can be used in pure functional program via monads (such as Haskell ST monad) or unique types (in Clean).
Bulat's user avatar
  • 1,898
1 vote
Accepted

Formal semantics of a mutable/imperative stack

After discussing it with specialists, a standard approach would be to denote the state of the mutable stack with an immutable stack in the Hoare triples. E.g. : $[]\ s\gets create() \ [s = \mathtt{[]...
ysalmon's user avatar
  • 233
1 vote

The idea behind the state of Imperative languages

The state of a program is all the information that is needed at runtime to determine what each line does. Consider a simple example that needs no state: ...
Cort Ammon's user avatar
  • 3,351
1 vote

Why don't imperative languages like C or Go support Haskell-like parametric polymorphism?

As others have pointed out, there is nothing preventing an imperative language from using something similar to the Hindley-Milner type system and providing parametric polymorphism. I should point out ...
Djzin's user avatar
  • 139

Only top scored, non community-wiki answers of a minimum length are eligible