Skip to main content
152 votes

How functional programming achieves "No runtime exceptions"

How does a Function Programming, such as Elm, achieve "No runtime exceptions"? That's easy. You simply don't write functions that fail. That might sound simplistic, but that's the gist of ...
Jörg W Mittag's user avatar
107 votes
Accepted

Return considered harmful? Can code be functional without it?

If a function doesn't have any side effects and it doesn't return anything, then the function is useless. It is as simple as that. But I guess you can use some cheats if you want to follow the letter ...
JacquesB's user avatar
  • 62.3k
60 votes

Do you need to think about encapsulation if you can ensure immutability?

I hate how encapsulation is always framed as preventing unauthorized access. If this were the best way to think of it, immutability would indeed eliminate most of the need for encapsulation. In fact, ...
Karl Bielefeldt's user avatar
56 votes
Accepted

Why are lists the data structure of choice in functional languages?

Because lists are simpler than trees. (You can see this trivially by the fact that a list is a degenerate tree, where every node has only a single child.) The cons list is the simplest possible ...
Jörg W Mittag's user avatar
54 votes
Accepted

Do you need to think about encapsulation if you can ensure immutability?

The question Casting your question to real life: Is it okay for your doctor to post your private medical records publicly to Facebook, provided no one (other than you) is able to change it? Is it ...
Flater's user avatar
  • 59.5k
46 votes

What are the functional equivalents of imperative break statements and other loop checks?

The closest equivalent to looping over an array in most functional languages is a fold function, i.e. a function that calls a user-specified function for each value of the array, passing an ...
Jules's user avatar
  • 17.9k
45 votes

Are immutable objects important only in multi-threaded applications and if so, how are shared immutable objects useful?

No, immutable objects are quite useful in general. The first and most basic reason is that concurrency in a system doesn't require a multi-threaded application. Making say... a row in a database ...
Telastyn's user avatar
  • 110k
40 votes

Return considered harmful? Can code be functional without it?

Tell, Don't Ask comes with some fundamental assumptions: You're using objects. Your objects have state. The state of your objects affects their behavior. None of these things apply to pure functions. ...
Robert Harvey's user avatar
39 votes

Is there a non-deterministic function without side effects?

Of course this depends on the definitions. Let's drop "pure" which has a definition in your question that clearly makes non-deterministic pure functions impossible as being deterministic is ...
Hans-Martin Mosner's user avatar
37 votes
Accepted

How do compilers work in a language that doesn't allow recursion?

Recursion can only be programmed either by having a call to function A within the definition of A itself (direct), or by having function A call function B, and function B call function A (indirect). ...
Kilian Foth's user avatar
34 votes
Accepted

Definition of "functor"; Haskell vs. C++

The two meanings are unrelated. The Haskell community (and really the Functional Programming community in general, and even the general programming community beyond FP) uses the term Functor in the ...
Jörg W Mittag's user avatar
33 votes

What are the functional equivalents of imperative break statements and other loop checks?

You could easily convert it to recursion. And it has nice tail-optimized recursive call. Pseudocode : public int doSomeCalc(int[] array) { return doSomeCalcInner(array, 0); } public int ...
Euphoric's user avatar
  • 38.2k
33 votes

Do you need to think about encapsulation if you can ensure immutability?

Encapsulation could mean that you hide the actual storage of immutable data. E.g.: class Color { private readonly uint argb; public byte Blue => (byte)(argb & 0xFF); public Color(byte ...
Emond's user avatar
  • 1,258
30 votes

Return considered harmful? Can code be functional without it?

When I deal with an object I don't ask it about its internal state. I tell it what I need to be done and it uses its internal state to figure out what to do with what I've told it to do. You don't ...
Timothy Truckle's user avatar
29 votes
Accepted

Do functional programming languages disallow side effects?

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 ...
amon's user avatar
  • 136k
27 votes

How do compilers work in a language that doesn't allow recursion?

To support recursion, a language needs to support function calls and a call stack. When a language doesn't allow recursion, it's typically because the language lacks one of these features. I'm not ...
JacquesB's user avatar
  • 62.3k
26 votes
Accepted

Is there a non-deterministic function without side effects?

A properly working classical computer is, by definition, deterministic. That is, the output of the same series of steps with the same inputs will produce the same result. When we talk about non-...
JimmyJames's user avatar
  • 30.9k
20 votes
Accepted

Sum Types vs Polymorphism

Like you, I wish that discriminated unions were more prevalent; however, the reason they are useful in most functional languages is that they provide exhaustive pattern matching, and without this, ...
VisualMelon's user avatar
20 votes

How functional programming achieves "No runtime exceptions"

How the Functional Paradigm or programming approach eliminates runtime exceptions? Elm does it by encoding return values as Maybe or Result instead of causing a runtime error. type Result error value ...
Robert Harvey's user avatar
20 votes

Definition of "functor"; Haskell vs. C++

The word "functor" has been around for a very long time, and modern usage stems from analytic philosophy, especially topics related to logic and language. The first usage I found was due to ...
Levi Pearson's user avatar
20 votes
Accepted

Is ad-hoc polymorphism a good practice in functional programming?

It seems to be a common cargo cult today to ask if something is "a good practice". Usually, this is the wrong question, since in programming there is almost nothing "good" or "...
Doc Brown's user avatar
  • 220k
19 votes

Return considered harmful? Can code be functional without it?

If you consider return as "harmful" (to stay in your picture), then instead of making a function like ResultType f(InputType inputValue) { // ... return result; } build it in a ...
Doc Brown's user avatar
  • 220k
19 votes
Accepted

Sample code to explain Banana Monkey Jungle problem by Joe Armstrong

He is hinting at a fact, that majority of real OOP programs don't respect separation of concerns. For example, you could have classes: public class Banana { public Monkey Owner {get;} } public ...
Euphoric's user avatar
  • 38.2k
19 votes
Accepted

Why usage of assignment operator or loops discouraged in functional programming?

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 ...
Frax's user avatar
  • 1,874
19 votes
Accepted

Best Practice - Wrapping if around function call vs Adding early exit if guard in function

Wrapping an if around a function call: This is to decide if the function should be called at all, and is part of the decision making process of your program. Guard clause in function (early return): ...
Baldrickk's user avatar
  • 734
18 votes

Sample code to explain Banana Monkey Jungle problem by Joe Armstrong

Gorillas are not monkeys! Leaving that aside, you answer your own question with "we can encapsulate all the logic behind the function 'getBanana'". All I want is a banana, but to get it I need to ...
David Arno's user avatar
  • 39.6k
18 votes
Accepted

Functional programming - what to learn and who uses it

Since you write that you're a .NET developer and you don't even mention F#, odds are that you're a C# developer. In that case, I'd strongly suggest that you learn F# first. It's another .NET language, ...
Mark Seemann's user avatar
  • 3,930
17 votes

Are immutable objects important only in multi-threaded applications and if so, how are shared immutable objects useful?

Immutable objects are useful independently of multithreading: They prevent very nasty bugs with reference types (e.g. classes in Java and C#), when these are used in the implementation of value ...
Christophe's user avatar
  • 82.1k
17 votes

How functional programming achieves "No runtime exceptions"

It doesn't remove errors. It just uses the type system to force you to handle all errors explicitly. To understand Maybe types, you had to go back to older languages like C that doesn't have Exception....
Lie Ryan's user avatar
  • 12.5k
16 votes

Is OOP really beneficial for enterprise-scale business software compared to procedural languages like ABAP or COBOL?

I strongly suggest you critically review your pain points and reflect how much of this is attributable to OOP inherently, versus inexperience stemming from what is clearly a workplace that is not ...
Flater's user avatar
  • 59.5k

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