Skip to main content
Share Your Experience: Take the 2024 Developer Survey
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
34 votes
Accepted

Could Hindley-Milner inference work for the Go language?

The Hindley-Milner type inference is used for Hindley-Milner type systems, a restriction of System-F type systems. The interesting feature of HM type systems is that they have parametric polymorphism (...
amon's user avatar
  • 134k
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.5k
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,910
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
  • 109k
9 votes
Accepted

Why isn't Bounded a subclass of Enum in Haskell

One practical example I like comes from the world of programming languages: the set of types in an OO system is bounded and discrete but not enumerable, and partially ordered but not totally ordered. ...
Benjamin Hodgson's user avatar
9 votes
Accepted

What is the difference between Applicative and Generative Modules, and Type Classes?

Since both are in OCaml, We can explore the difference between generative and applicative functors easily: module type S = sig type t end module M = struct type t = int end (* An Applicative functor....
Drup's user avatar
  • 204
8 votes

Difference between Python classes and Haskell typeclasses

One difference: In Python, you can define a subclass MySubclass of MyClass, and then you can freely add objects of MyClass with objects of MySubclass. Haskell tends to avoid subtyping: if you have a ...
Daniel Díaz Carrete's user avatar
8 votes

Can I use maybe on multiple inputs in Haskell?

The code is morally correct but lacking in a few places. The conditional stuff is actually fine, the part that's tripping you up is Indicating that you want a tuple Constructing a Maybe value First ...
daniel gratzer's user avatar
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,231
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
  • 59.7k
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
  • 109k
7 votes
Accepted

Casting a nullary data constructor between types

One thing you could do is change the definition of your datatype to factour out the Valid / Invalid part like so: data Status = StatusValid | StatusInvalid data Check a = Sure Status | Unsure a You ...
gallais's user avatar
  • 215
7 votes
Accepted

Type of map for Try[T]

Scala is an object-oriented language. In object-oriented languages, methods have special access to a designated "receiver" argument (typically called this or self). This parameter is usually not made ...
Jörg W Mittag's user avatar
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
  • 134k
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,227
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

Types in Lisp and Scheme

Typed Racket is very different from Haskell. Type systems in Lisp and Scheme, and indeed type systems in traditionally untyped language ecosystems in general, have a fundamental goal that other type ...
Jack's user avatar
  • 4,499
6 votes
Accepted

What is a Haskell value

All these terms - expressions, values, and "class" - are general PL concepts that have no specific ties to Haskell, and are best understood under a more general framework. To keep things brief, I will ...
gardenhead's user avatar
  • 4,747

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