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 ...
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 ...
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 ...
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 ...
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 ...
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, ...
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 ...
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 ...
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 ...
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 ...
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 ...
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 ...
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 ...
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 ...
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 ...
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 ...
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 ...
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 ...
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
+---+--...
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 ...
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 ...
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).
<*> :: ...
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
...
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 ...
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&...
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 ...
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.
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, ...
Only top scored, non community-wiki answers of a minimum length are eligible
Related Tags
haskell × 260functional-programming × 99
type-systems × 21
monad × 16
programming-languages × 13
scala × 13
clojure × 13
java × 11
data-structures × 9
design-patterns × 8
python × 8
lisp × 8
scheme × 8
design × 7
erlang × 7
typeclass × 7
c# × 6
algorithms × 6
polymorphism × 6
recursion × 6
c++ × 5
data-types × 5
object-oriented × 4
javascript × 4
programming-practices × 4