Questions tagged [imperative-programming]
The imperative-programming tag has no usage guidance.
32
questions
19
votes
11
answers
5k
views
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. ...
0
votes
2
answers
153
views
Declarative, interrogative, imperative, and exclamative sentences in computer languages
The following English sentences have different forms (syntax):
Declarative: You are my friend.
Interrogative: Are you my friend?
Imperative: Be my friend!
Exclamative: What a good friend you are!
...
0
votes
1
answer
31
views
Example on Referential transparency (wikipedia)
I have a rather foolish question on an example
explaining the idea behind Referential transparency
Here is given an example i not understand:
Consider a function that returns the input from some ...
3
votes
2
answers
110
views
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 ...
1
vote
1
answer
123
views
Is object oriented programming considered to be a declarative programming approach?
Is object oriented programming considered to be a declarative programming approach?
On Wikipedia, it is said that OOP tries to extend the imperative programming paradigm to a somewhat declarative ...
1
vote
1
answer
131
views
For every imperative function, is there a functional counterpart with identical performance or even instructions?
Currently, I haven't learned about a functional language that can achieve the same performance as C/C++. And I have learned that some languages that favor functional programming to imperative ...
0
votes
0
answers
16
views
Denotational semantics of command sequencing
Moved from cstheory.stackexchange upon request
Winskel, in his book The formal semantics of programming languages, on page 58, writes:
C[c0;c1] = C[c1] o C[c0] a composition of relations, the ...
1
vote
1
answer
63
views
Is "imperative computing" just a fancy way of saying "scripting"? Or is there more to it?
Maybe this is a very ignorant question. I don't understand imperative computing very well, would it be correct to think of it as simply scripting (as in, running one or a few commands at a time, ...
2
votes
2
answers
168
views
Is purely functional programming in some situations less efficient than imperative programming?
I am used to implementing algorithms in imperative languages. Many of the algorithms I have implemented use hash maps, hash sets, mutable arrays, heaps, doubly linked lists, etc. I understand that ...
3
votes
1
answer
35
views
Formal semantics of a mutable/imperative stack
When introducing formal semantics for data structures, immutable stacks are a nice simple example :
$\mathit{is\_empty}(\mathit{create()})=\mathrm{True}$
$\mathit{is\_empty}(\mathit{push}(e, s))=\...
3
votes
1
answer
39
views
"Immediate" method of translating arbitrary mutable program to equivalent unmutable program?
Consider the following program in "mutable style":
x=1
x=f(x);
x=f(x);
We can rewrite this in "immutable style" without changing the higher-level structure of ...
3
votes
2
answers
379
views
The idea behind the state of Imperative languages
I was reading about programming paradigms and I have a question about the state when speaking about Imperative languages. What does it mean to change the program's state? Is it just an abstract idea? ...
5
votes
1
answer
218
views
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:
...
0
votes
2
answers
339
views
Definition of "idempotence" of a function?
Currently there are two things that may change between invocations of a function: the return value, and the external states. I understand that if the external states changes, the function is not ...
4
votes
1
answer
444
views
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.
...
5
votes
2
answers
531
views
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 ...
2
votes
1
answer
636
views
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 ...
0
votes
0
answers
44
views
What is the origin of state concept?
On a Turing machine, the state changes during the execution of the program. Von Neumann architecture implements this machine theory and typical computer hardware uses Von Neumann architecture.
...
2
votes
0
answers
799
views
What is the difference between a loop and a natural loop?
Is there a difference between a loop and a natural loop? Natural loops are defined as having a back edge to a head block that dominates it. If you have a natural loop nested inside of another ...
3
votes
0
answers
75
views
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 ...
24
votes
1
answer
2k
views
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 ...
3
votes
1
answer
378
views
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 ...
3
votes
2
answers
48
views
Is there a formal term for functions that have static state across executions?
Two examples, one in PHP:
function adder($i){
static $a = 0;
$a += $i;
return $a;
}
A similar effect can be achieved with closures in javascript:
...
6
votes
2
answers
221
views
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:
...
2
votes
1
answer
383
views
Do functional algorithms require more memory than imperative algorithms? [closed]
Let's suppose we are counting words in string. We split it so what we have is an array of strings. I'll use Python as an example.
The imperative approach would as follows:
...
4
votes
1
answer
732
views
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 ...
1
vote
1
answer
525
views
Loop invariant: decreasing a variable and swapping it
As an example exercise, we were asked to find the loop invariant for this bit of code.
...
8
votes
2
answers
2k
views
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 ...
1
vote
1
answer
645
views
Asymptotic time complexity of a two-loop program
I have two pieces of code in a function which I'm trying to calculate the asymptotic running time for:
...
7
votes
2
answers
20k
views
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 ...
5
votes
1
answer
297
views
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$ ...
5
votes
1
answer
845
views
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 ();
(...