Questions tagged [imperative-programming]

Imperative programming is a paradigm of expressing the logic of a computer program or computation by explicitly describing its control flow in terms of statements that change a program state.

From Wikipedia:

In computer science, imperative programming is a programming paradigm that describes computation in terms of statements that change a program state. In much the same way that imperative mood in natural languages expresses commands to take action, imperative programs define sequences of commands for the computer to perform.

FAQ:

See also

143 questions
11
votes
1 answer

Fast impertive pointers (static, unboxing, etc.) with Struct library

I am interested in using more efficient pointers for a project implementing an imperative language in Haskell. There is already a library for that: Struct. There is a blog post on it and brief documentation. The problem is there is only a quite…
mrsteve
  • 4,082
  • 1
  • 26
  • 63
11
votes
3 answers

Replacing functions with Table Lookups

I've been watching this MSDN video with Brian Beckman and I'd like to better understand something he says: Every imperitive programmer goes through this phase of learning that functions can be replaced with table lookups Now, I'm a C# programmer…
Jamie Dixon
  • 53,019
  • 19
  • 125
  • 162
10
votes
4 answers

Idiomatic exceptions for exiting loops in OCaml

In OCaml, imperative-style loops can be exited early by raising exceptions. While the use of imperative loops is not idiomatic per se in OCaml, I'd like to know what are the most idiomatic ways to emulate imperative loops with early exits (taking…
anol
  • 8,264
  • 3
  • 34
  • 78
9
votes
9 answers

Is functional programming a subset of imperative programming?

One of the main characteristics of functional programming is the use of side-effectless functions. However, this can be done in an imperative language also. The same is true for recursion and lambda functions (for example C++0x). Therefore I wonder…
7
votes
9 answers

Translate imperative control flow with break-s/continue-s to haskell

Consider the following imperative code which finds the largest palindrome among products of 3-digit numbers (yes, it's the one of the first tasks from "Project of [outstanding mathematician of 18th century]" site): curmax = 0 for i in…
dorserg
  • 5,454
  • 3
  • 24
  • 29
7
votes
3 answers

Why functional programming language support automated memoization but not imperative languages?

This is a question I read on some lectures about dynamic programming I randomly found on the internet. (I am graduated and I know the basic of dynamic programming already) In the section of explaining why memoization is needed, i.e. // psuedo code…
shole
  • 4,046
  • 2
  • 29
  • 69
6
votes
4 answers

Why don't imperative languages have pattern matching?

So, pattern matching in functional languages is pretty awesome. I wondering why most imperative languages haven't implemented this feature? To my understanding, Scala is the only "mainstream" imperative language that has pattern matching. The…
mollerhoj
  • 1,298
  • 1
  • 10
  • 18
6
votes
5 answers

Are programs in functional languages more likely to have stack overflows?

I am starting to learn ocaml, and am really appreciating the power of recursion in the language. However, one thing that I am worried about is stack overflows. If ocaml uses the stack for function calls, won't it eventually overflow the stack? For…
5
votes
2 answers

Explain the algorithm to solve 'longest increasing subsequence' problem

I have been trying to understand this algorithm for past two hours, but can't seem to get it. Can someone please explain it in easy to understand manner? function lis_length(a) n := a.length q := new Array(n) for k from 0 to n: …
missingfaktor
  • 90,905
  • 62
  • 285
  • 365
5
votes
2 answers

Functional way to loop over nested list

I was given a question to compare two trees. Something like below: case class Node(elem:String, child:List[Node]) In order to compare each elements of the trees, I have following functions: def compare(n1:Node, n2:Node): Boolean { if(n1.elem ==…
5
votes
1 answer

OCaml: Throw away return value in imperative code

How do I successfully throw away the return value of a function and treat it as if it's returning unit (for side-effects, obviously). The obvious approach is to do this: let foo = begin some_ignored_thing (); actual_return end However, the…
Impredicative
  • 5,039
  • 1
  • 17
  • 43
5
votes
2 answers

Does runtime generally use an imperative-like interpretation of functional language code

I have a general question about interpreters of functional languages: Are there actually any advantages to using a functional language versus an imperative language at runtime (or that make their way to the interpreter)? None of the questions I saw…
4
votes
4 answers

Convert recursive binary tree traversal to iterative

I was asked to write the iterative version, but I wrote the recursive version i.e. void inorderTraverse(BinaryTree root) { if(root==NULL) printf("%d",root->id); else { inorderTraverse(root->left); …
Kraken
  • 23,393
  • 37
  • 102
  • 162
4
votes
6 answers

Is there a functional algorithm which is faster than an imperative one?

I'm searching for an algorithm (or an argument of such an algorithm) in functional style which is faster than an imperative one. I like functional code because it's expressive and mostly easier to read than it's imperative pendants. But I also know…
kiritsuku
  • 52,967
  • 18
  • 114
  • 136
4
votes
2 answers

SICP - Imperative versus Functional implementation of factorial

I am studying the SICP book with Racket and Dr. Racket. I am also watching the lectures on:…
user4206400
1
2
3
9 10