Questions tagged [imperative-programming]
34 questions
25
votes
1 answer
Does a do-while loop suffice for Turing-completeness?
I know that, in imperative programming languages, a while-do loop is sufficient as a control flow construct to make the language Turing-complete (as far as control flow goes - of course we also need unbounded memory and certain operators...). The…
Martin Ender
- 663
- 6
- 16
19
votes
11 answers
Why do functional languages disallow reassignment of local variables?
Fair warning: I don't actually know a functional language so I'm doing all the pseudocode in Python
I'm trying to understand why functional languages disallow variable reassignment, e.g. x = x + 1. Referential transparency, pure functions, and the…
BatWannaBe
- 365
- 2
- 9
8
votes
2 answers
Isn't Functional Programming just Imperative Programming in disguise?
A YouTube video I was watching explained the differences between Imperative and Functional programming by demonstrating how the numbers from 1 to 10 are summed up in Java and in Haskell respectively.
In Java, you must explicitly state each step and…
CodyBugstein
- 3,017
- 11
- 31
- 46
7
votes
2 answers
Is it possible to find the nth term of a Fibonacci sequence using a definitive for loop?
I'm using the book Introduction to Computer Science by John Zelle and at the end of Chapter 3 (Computing with numbers), I'm asked to find the nth term of a Fibonacci sequence presumably using a definitive for loop, as no other decision structure has…
qzxt
- 79
- 1
- 1
- 3
6
votes
2 answers
Eliminate non-local references from closure
For a code similarity detection framework I need to eliminate references to non-local variables, for example having the following closure:
{
var a;
var b;
var f = function() {
a = 5;
b = 6;
}
f();
}
would be converted into…
Philip Kamenarsky
- 163
- 2
5
votes
1 answer
Best way to translate while loops to functions for software verification
I have a verification engine where while loops are translated into functional code, like this:
def f() = ... while(c) b ...
def f() = ... fwhile() ...
def fwhile() = if(c){ fwhile() }
We want for the users to be able to…
user1868607
- 2,224
- 14
- 23
5
votes
2 answers
Why don't imperative languages like C or Go support Haskell-like parametric polymorphism?
Why don't imperative languages support parametric polymorphism as powerful as whats in Haskell and OCaml?
More specifically if I call a function foo(x) that internally calls bar(x) which internally calls baz(x), what prevents the type of parameter x…
hgs3
- 283
- 1
- 9
5
votes
1 answer
Double-nested loop with bitwise operation
I have this little exercise:
for ( i = 0; i < 2 * n; i += 2 )
for ( j = 1; j <= n; j <<= 1 )
if ( j & i )
foo ();
(j <<= 1 means j = (j << 1), where << is the bitwise left shift operator. & is the bitwise and operator.)
I am supposed to…
Machta
- 151
- 2
5
votes
1 answer
termination of two concurrent threads with shared variables
We're in a shared memory concurrency model where all reads and writes to integer variables are atomic.
do: $S_1$ in parallel with: $S_2$ means to execute $S_1$ and $S_2$ in separate threads, concurrently.
atomically($E$) means to evaluate $E$…
Gilles 'SO- stop being evil'
- 44,159
- 8
- 120
- 184
4
votes
1 answer
Can we add dependent type into an existing imperative programming language?
As we know, dependent type allows programmers to write bugless programs.
But as I know there's only very few languages support dependent type, like Haskell with extensions, Idris, Agda, F*, etc.
Since these are all functional languages, is there any…
ice1000
- 980
- 6
- 35
4
votes
1 answer
Denotational semantics of expressions with side effects
I'm doing revision for a module on programming language semantics and
I'm having trouble understanding the introduction of side-effects in
expressions.
We assume a standard syntax for arithmetic expression, with the four
usual operations, plus the…
user2320239
- 235
- 2
- 2
4
votes
2 answers
Plain-language example of how a functional style makes parallel programming easier
I read a few "function >> imperative/OOP" articles because I heard there was a move in imperative OOP languages toward a functional style of coding, especially encouraging pure functions when possible. One recurring argument is that by not mutating…
BatWannaBe
- 365
- 2
- 9
3
votes
1 answer
An imperative program is analogous to how a Turing machine works?
Since Turing machines has great influence on typical hardware architecture (Von Neumann) and both uses concept of state, is correct to say that an imperative program is analogous to how a Turing machine works?
“Computability via Turing machines…
Alexandre Demelas
- 145
- 4
3
votes
0 answers
Definition of imperative semantic in a HDL context
I've been studying from this book the topic of "Synthesis and optimization of digital circuit". In chapter 3 some theory about programming languages is exposed. I was wondering what's the meaning of this paragraph about the meaning of "imperative…
user8469759
- 723
- 3
- 19
3
votes
1 answer
Why precondition strengtening is sound in Hoare logic
I have problem with understanding why precondition strengthening is sound rule in Hoare logic. The rule is:
$$
{P \implies Q, \{Q\} C \{X\} } \over {\{P\} C \{X\}}
$$
I really do not understand why precondition in consequence goes in reverse…
Trismegistos
- 162
- 5