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
4
votes
2 answers

Observer pattern forces imperative style

I was looking at the Reactive Programming course from Coursera, which uses Scala to implement an Observer pattern. There, Martin Odersky says that the Observer pattern forces Imperative Programming, which we can see because the subscriber's handlers…
bsky
  • 19,326
  • 49
  • 155
  • 270
4
votes
4 answers

Stateful computation with different types of short-circuit (Maybe, Either)

I am trying to find the most elegant way of converting the following stateful imperative piece of code to pure functional representation (preferably in Haskell to use abstraction that its Monad implementation offers). However I am not yet good at…
4
votes
5 answers

Translating a simple imperative algorithm to functional style

I recently made a small algorithm to strip out function arguments from a snippet of code and only keep the outermost functions. I found that this algorithm was very easy to design in an imperative way. However, I'm really interested in functional…
4
votes
7 answers

How to remove imperative code from a function?

I'm new to functional world and appreciate help on this one. I want to SUPERCEDE ugly imperative code from this simple function, but don't know how to do it. What I want is to randomly pick some element from IEnumerable (seq in F#) with a respect to…
DinGODzilla
  • 1,611
  • 4
  • 21
  • 34
4
votes
1 answer

Mutable fields of records with multiple files

I'm working with multiple files, and i have a problem with one mutable field. In file1.ml, i declared: type mytype = { mutable numbers : int list; } So, in file2.ml, i have elements of type mytype. But, when i'm trying to…
4
votes
6 answers

disadvantage of pure function in functional programming

A pure function in functional programming is the one which do not have side effects. One of the meanings of this is it can not change the values of input parameters. Can this be seen as disadvantage for the memory utilization? e.g. Lets say I have…
Manoj R
  • 3,197
  • 1
  • 21
  • 36
4
votes
3 answers

Imperative languages with static, structural typing and global type inference

I know of languages like Haskell being statically typed and having type inference. But are there non-functional languages that have global type inference, the equivalent of something like C with type inference and structural typing.
4
votes
1 answer

Converting an imperative algorithm that "grows" a table into pure functions

My program, written in Python 3, has many places where it starts with a (very large) table-like numeric data structure and adds columns to it following a certain algorithm. (The algorithm is different in every place.) I am trying to convert this…
max
  • 49,282
  • 56
  • 208
  • 355
4
votes
2 answers

Imperative vs Functional by example

I have a method implemented in imperative and functional (I did my best here) ways. The method iterates over ArrayBuffer[Creature], calculates distance to each creature and return the closest or None (if no creatures in the world except…
Soteric
  • 3,070
  • 5
  • 24
  • 23
3
votes
2 answers

Stuck on a Concurrent programming example, in pseudocode(atomic actions/fine-grained atomicity)

My book presents a simple example which I'm a bit confused about: It says, "consider the following program, and assume that the fine-grained atomic actions are reading and writing the variables:" int y = 0, z = 0; co x = y+z; // y=1; z=2; oc; "If x…
Caffeinated
  • 11,982
  • 40
  • 122
  • 216
3
votes
1 answer

Updating an outer variable in Haskell

There is a sense in which Haskell is a purely functional language, and certainly, idiomatic code tries to be as functional as reasonably possible. At the same time, Haskell does provide support for fairly direct translation of some imperative…
rwallace
  • 31,405
  • 40
  • 123
  • 242
3
votes
1 answer

OCaml variable, which keeps it value between function calls

Is there a way in OCaml for a variable inside a function to keep its value between function calls? It should work like Pythons default argument, which is a reference to the same object in every function call or the function should rather yield and…
user5987748
3
votes
1 answer

Imperative vs Functional - understanding the von Neumann bottleneck

In the Coursera course - Functional Programming in Scala - Martin Odersky talks about how Imperative Programming is constrained by the von Neumann bottleneck because it deals largely with mutable state and also, therefore, assignment and…
3
votes
3 answers

What is meant by imperative and interrogative code

My Manager asked me to code in ASP.net. What is meant my imperative and interrogative code. How it related to programmers?
3
votes
2 answers

Factorial using imperative-style programming

I have the following code: while :: IO Bool -> IO () -> IO () while test body = do b <- test if b then do {body ; while test body} -- same-line syntax for do else return () I need to implement the factorial function using…
user3055141
  • 109
  • 1
  • 2
  • 8
1 2
3
9 10