62

Referential transparency, referred to a function, indicates that you can determine the result of applying that function only by looking at the values of its arguments. You can write referentially transparent functions in any programming language, e.g. Python, Scheme, Pascal, C. On the other hand, in most languages you can also write non referentially ...


32

If I understand correctly, the development process was peer review and experimentation. The team consisted of people like "Math Doctors" - extremely dedicated, intelligent, passionate, detail-oriented folks whose lives were dedicated to their work. So when I say peer review, I mean many peer reviews over the course of many months (more than a year). These ...


26

Functional programming includes many different techniques. Some techniques are fine with side effects. But one important aspect is equational reasoning: If I call a function on the same value, I always get the same result. So I can substitute a function call with the return value, and get equivalent behaviour. This makes it easier to reason about the program,...


25

def f(x): return x() from random import random f(random) + f(random) == 2*f(random) # => False However, that's not what Referential Transparency means. RT means that you can replace any expression in the program with the result of evaluating that expression (or vice versa) without changing the meaning of the program. Take, for example, the following ...


23

All programming languages and programs are generalized abstractions. We simulate those abstractions on a machine that does things its own way. Were we confined to the way a computer works, we would all be using languages like C or Forth, which are "close to the metal" languages. Computers are general-purpose machines; we are free to create whatever ...


21

The basic unit of an imperative program is the statement. Statements are executed for their side effects. They modify the state they receive. A sequence of statements is a sequence of commands, denoting do this then do that. The programmer specifies the exact order to perform the computation. This is what people mean by telling the computer how to do it....


16

The declarative code is harder to debug. I would say that is a function of the quality of your debugger. If your debugger understands the imperative constructs but not the declarative ones, then of course the delarative ones are harder to debug. But you could easily imagine a different debugger with different priorities, where the opposite is true. There ...


16

What is it in functional programming that makes a difference? Functional programming is by principle declarative. You say what your result is instead of how to compute it. Let's take a look at really functional implementation of your snippet. In Haskell it would be: predsum pred numbers = sum (filter pred numbers) Is it clear what the result is? Quite so,...


15

I would write the if-statement slightly different, so it is taken when the input is successful. for (;;) { cout << ": "; if (cin >> input) break; cin.clear(); cin.ignore(512, '\n'); } It's shorter as well. Which suggests a shorter way that might be liked by your teacher: cout << ": "; while (!(cin >> input))...


15

What you have to strive for is avoiding raw loops. Move the complex logic into a helper function and suddenly things are a lot clearer: bool getValidUserInput(string & input) { cout << ": "; cin >> input; if (cin.fail()) { cin.clear(); cin.ignore(512, '\n'); return false; } return true; } int ...


15

The AGC is controlled with verbs and nouns The Apollo command software is not written in any syntax users would recognize today. Astronauts input commands numerically, with each two-digit number representing a verb or a noun. The verb described the action to be performed, and the noun specified the data to be affected by the verb’s action. Astronauts hated ...


14

There was a lovely documentary I'm trying to chase down about John 'Jack' Garman had to "invent" a "a priority-scheduled multiprogramming operating system". This may have been related to the lander module though. The story was that when you were landing the lander, you better give priority to guidance because other things, like the temperature in the cabin ...


13

If by functional programming you mean programming only with immutable values, sure, you can do that. But it's going to be painful. In a lot of cases you don't get to take advantage of: First-class functions with lexical scoping (a.k.a. closures) Functions with identifiers that are mostly special characters Infix functions Type inference Tail call ...


13

Horner's method is probably more computationally efficient as @delnan points out, but I would call this pretty readable in Python for the exponentiation solution: def eval_poly(poly, x): return sum( [a * x**i for i,a in enumerate(poly)] )


11

Functional programming is just too complicated for inexperienced programmers. One of the illustrations is this one, where students declared that 30-LOC mess was easier and more intuitive to understand compared to the four lines FP analog. (This is the original version of the FP example, as the answer in the link has been more recently edited to make it ...


11

Programming languages have two purposes. The first is easy to see, "programming languages have to tell a computer what to do." The second is more subtle: "programming languages have to allow a programmer to convey to the computer what they want it to do, efficiently." In theory, one could always program in assembly (or even machine code), and tell a ...


9

It's not so much that for(;;) is bad. It's just not as clear as patterns like: while (cin.fail()) { ... } Or as Sjoerd put it: while (!(cin >> input)) { ... } Let's consider the primary audience for this stuff as being your fellow programmers, including the future version of yourself who no longer remembers why you stuck the break at the ...


9

Suppose that you use and create only pure functions. These by definition have no shared state and are easy to compose, and there's no way to use a goto. This satisfies both 'modularity' and 'structured control flow' properties. What could it be? A map-reduce calculation. A Prolog inference. An SQL query (can be reused as a subquery or a view). Are you ...


9

The use of mutable state is generally discouraged in functional programming. Loops are discouraged as a consequence, because loops are only useful in combination with mutable state. The function as a whole is pure, which is great, but the paradigm of functional programming does not only apply at the level of whole functions. You also want to avoid mutable ...


8

Like pretty much every other software project, it was done under aggressive deadline and quality pressure. Fortunately a large archive of material from software project manager Howard W. “Bill” Tindall, Jr. is available here. If you sample the memos, you can get a very good sense of the normal conflict between time, features, and defects. It's worth noting ...


7

Many functional languages have mapi implementations that allow to you have an index weaved through a map. Combine that with a sum and you have the following in F#: let compute coefficients x = coefficients |> Seq.mapi (fun i c -> c * Math.Pow(x, (float)i)) |> Seq.sum


7

The real defining characteristic that separates declarative from imperative programming is in declarative style you are not giving sequenced instructions; at the lower level yes the CPU operates this way, but that's a concern of the compiler. You suggest CSS is a "declarative language", I wouldn't call it a language at all. It's a data structure format, ...


7

While you are correct that from an external observer's point of view, your Sum function is pure, the internal implementation is clearly not pure - you have state stored in result which you repeatedly mutate. One of the reasons to avoid mutable state is because it produces a greater cognitive load on the programmer, which in turn leads to more bugs[citation ...


6

If you want a good example of full-fledged OOP in C, take a look at GTK+. Mostly, your constructors have a lot of boilerplate to initialize things like vtables and keep track of a type hierarchy. Creating your own object for GTK+ is a good way to learn what most OO languages do for you behind the scenes. One thing you'll learn by writing some real code is ...


6

Well, you already hinted at the answer. Imperative programming is closer to the metal, so it makes more sense in places (like embedded) where you're working closer to the metal. Nobody would bother programming an Arduino in Haskell (well, except maybe for @JimmyHoffa), though programming one in Scheme is not unheard of. Some other reasons: Computing model ...


6

I know I'm late to the party, but I had an epiphany the other day so here it goes... I think comments about functional, immutable and no side-effects miss the mark when explaining the difference between declarative vs imperative or explaining what declarative programming means. Also, as you mention in your question, the whole "what to do" vs "how to do it" ...


5

You already said in your question how you can tell whether your program is referentially transparent or not: if you can replace a expression with its value, without changing the meaning of the program, it is referentially transparent. If you cannot do that, it is not. So, let's just try and replace some expressions with their values! in your summation ...


5

The only good way to encourage others to work a certain way, is to demonstrate the benefits of doing so. You could also force people to work a certain way, via code reviews, but if the people don't understand the benefits, you will simply make everyone unhappy. Pure functions have plenty of benefits even in imperative languages: Predictability: with the ...


5

Structured programming is essentially programming by composing procedures. Can you do something similar with functions in a functional language? Yes. When people talk about structured programming, do they usually mean to include functional programming? No. If you say "structured programming style," will people understand you to be including functional ...


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