Skip to main content
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.8k
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
  • 37.8k
33 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
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
19 votes
Accepted

What is the reasoning behind making non-determinism a feature of Haskell?

While it is true that both aspects cited in the questions appear as forms of non-determinism, they are indeed quite different both in how they work and in their goals. Hence any answer has to be ...
Nicola Gigante's user avatar
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,920
14 votes

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

I really like Jules' answer, but I wanted to additionally point out something people often miss about lazy functional programming, which is that everything doesn't have to be "inside the loop." For ...
Karl Bielefeldt's user avatar
13 votes

What is the benefit of short readable code if you only see functions and classes on the outside?

how exactly the methods and properties were written you do not care about, as long as they do what they promise to do. Exactly. But as soon as they don't do what they promise to do, you have to go ...
Arseni Mourzenko's user avatar
11 votes
Accepted

Should I use functions based on Applicatives or Monads?

For both efficiency and generality, you should typically prefer the most general operators. So you should use Applicative operators in preference to Monad, and Alternative to MonadPlus. The ...
Derek Elkins left SE's user avatar
10 votes
Accepted

Precisely define "what to solve" and "how to solve" corollary in functional and imperative programming respectively

Isn't above code snippet is just different way of saying "how to solve" problem ? Yes. In the end, computer science is invariably about computing so you're always talking about "how to solve" a ...
Telastyn's user avatar
  • 110k
8 votes

What is the difference between Haskell's type classes and Go's interfaces?

There are several differences Haskell typeclasses are nominatively typed -- you have to declare that Maybe is a Monad. Go interfaces are structurally typed: if circle declares area() float64 and so ...
walpen's user avatar
  • 3,241
8 votes
Accepted

CQRS/ES in haskell, using "Out of the tar pit" paper architecture

With the CQRS/ES in mind, I have a decision engine. The decision engine for producing an event from a command, is the Business Domain, which has to be purely functional. Good. When a command asks ...
VoiceOfUnreason's user avatar
8 votes

How Functional Programming addresses concurrent increment/decrement operations invoked by different users?

You are asking how functional languages handles shared mutable state. Functional languages do prefer and encourage immutable data structures, but they all (AFAIK) have provisions for mutable shared ...
JacquesB's user avatar
  • 61k
8 votes

What is the benefit of short readable code if you only see functions and classes on the outside?

First, separate the issue of short code from readable code. While brevity can contribute to readability, it doesn't guarantee it. Nor does it guarantee efficiency of execution. So I was wondering ...
Charles E. Grant's user avatar
8 votes

In Haskell, is it a "violation" of functional programming to interact with something that was not a function parameter?

Is this now "impure"? Should the constant be something that is always passed to worlds? In your second example, the function worlds is actually a closure that references the variable constant defined ...
Giorgio's user avatar
  • 19.7k
8 votes

Functional programming - what to learn and who uses it

I have never personally met anyone who working with functional programming language in their day today job. I am paid to write OCaml, AMA! My main reservations are on whether Haskell is a good ...
Telastyn's user avatar
  • 110k
7 votes
Accepted

How is arrow operator an Applicative Functor in Haskell?

The type (->) r a is just an alias for r -> a, nothing more. Its kind is * (“type”). The type constructor (->) r can be thought of as r -> ___ where r is already known but ___ is to be ...
Rufflewind's user avatar
  • 2,245
7 votes
Accepted

What is Banana split and fusion in functional programming?

Even though 2 answers have already been provided, I don't think the "banana split" has been explained here yet. It is indeed defined in "Functional Programming with Bananas, Lenses, Envelopes and ...
Klaas van Schelven's user avatar
7 votes
Accepted

What is the difference between Haskell's type classes and Go's interfaces?

The two concepts are very very similar. In normal OOP languages, we attach a vtable (or for interfaces: itable) to each object: | this v +---+---+---+ | V | a | b | the object with fields a, b +---+--...
amon's user avatar
  • 135k
6 votes
Accepted

How do you encode Algebraic Data Types in a C#- or Java-like language?

All of the other answers here are outdated (as of me writing this answer). Java has first class support for Abstract Data Types (ADT's) now. The easiest way to create an ADT is by using Records and ...
davidalayachew's user avatar
6 votes

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

Most list processing examples you will see use functions like map, filter, sum etc. which operate on the list as a whole. But in your case you have a conditional early exit - a rather uncommon pattern ...
JacquesB's user avatar
  • 61k
5 votes

How is arrow operator an Applicative Functor in Haskell?

Let's use some equational reasoning. Our functor f is (->) r, so f a is (->) r a which is just r -> a. pure :: a -> f a, so in this case pure :: a -> (r -> a). <*> :: ...
Michael Shaw's user avatar
  • 5,126
5 votes

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

As pointed out by other answers, Clojure has reduced for stopping reductions early: (defn some-calc [coll] (reduce (fn [answer e] (let [answer (+ answer e)] (case answer ...
nilern's user avatar
  • 59
5 votes

What's the benefit of avoiding partial functions in Haskell?

Partial functions are bad because there are now illegal states in your software. The most elegant solution to avoid these is to make illegal states unrepresentable, for example by introducing a new ...
amon's user avatar
  • 135k
5 votes
Accepted

Imperative parallels to Haskell's Monad operations

If it helps you wrap your head around it, your intuition is a decent analogy for some monads. Consider, however, the list monad: Prelude> [1, 2, 3] >> [4, 5, 6] [4,5,6,4,5,6,4,5,6] Prelude&...
Karl Bielefeldt's user avatar
4 votes

Better way of logging than manual calls to a logging function?

Logging is a common use-case given during the presentation of so-called Free monads, Freer monads, and/or Extensible Effects. In any case, the basic idea is simple -- rather than having your program ...
Nathan Davis's user avatar
4 votes

How is it obvious that that (foldr Cons Nil) just copies a list?

Replacing Cons by Cons and Nil by Nil keeps things the same! In the given append example, [3, 4] is what replaces Nil.
pigworker's user avatar
  • 141
4 votes

What is the difference between Haskell's type classes and Go's interfaces?

They are completely different. Go interfaces define a protocol for values, Haskell type classes define a protocol for types. (That's why they are called "type classes", after all. They classify types, ...
Jörg W Mittag's user avatar

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