Questions tagged [functional-programming]

Functional programming is a programming paradigm based upon building abstractions using functions, avoiding side effects and change of state. Pure functional programming is thread-safe.

Functional programming is a programming paradigm that deals primarily with mathematical functions. In functional languages, functions are first-class values.

Functions take arguments and return results, but typically don't mutate state. This in contrast to , which primarily revolves around statements that change state. The advantage of avoiding mutable state is that you can safely compose functions, and you can use algebraic laws and "substitution of equals for equals" to simplify programs or improve their performance.

One consequence of this is that many common patterns in programming can be abstracted as higher-order functions, which use a user-supplied function that implements real functionality, and apply it to data in a certain way. This can make code more concise and simpler to reason about and understand.

Functional programming has grown out of a mathematical system called lambda calculus, which was developed in the 1930s. was the first programming language to be based on the lambda calculus.

Today, functional programming is getting more and more popular. The main reason for this is the provability of functional programs' properties, and security is very important nowadays. There are many use cases for functional programming, e.g. Computations or Concurrency handling. Use cases of functional programming.

Programming languages

These languages are listed in order of popularity in relation to the functional-programming tag.

Languages that are primarily functional, although some also support mutable state or other programming paradigms:

Languages that have some functional aspects (like support for first class functions) but are not considered functional languages per se:

History and concepts

The Conception, Evolution, and Application of Functional Programming Languages by Paul Hudak.

18902 questions
12
votes
0 answers

How to implement an optimal beta reduction on Levy's sense?

In 1990, John Lamping published a paper proposing an optimal implementation of the untyped lambda calculus. Since that paper is 25 years old, I wonder how much we have advanced since. Thus, my question is: what is a simple description of John's…
MaiaVictor
  • 51,090
  • 44
  • 144
  • 286
12
votes
2 answers

Iterate over all pair combinations without repetition in Haskell

In haskell, given a list of elements, xs, the simplest way to iterate over all pair permutations with repetitions is: [(x,y) | x <- xs, y <- xs] I wish to be able to do the same, but only on combinations. If x and y were comparable, I could…
tohava
  • 5,344
  • 1
  • 25
  • 47
12
votes
5 answers

Multiplying numbers on horizontal, vertial, and diagonal lines

I'm currently working on a project Euler problem (www.projecteuler.net) for fun but have hit a stumbling block. One of the problem provides a 20x20 grid of numbers and asks for the greatest product of 4 numbers on a straight line. This line can be…
untwisted
  • 271
  • 1
  • 8
12
votes
1 answer

Does return break referential transparency?

I was reading the description of the Scala WartRemover tool, and was confused by one of the points they had. The description said this: return breaks referential transparency. Refactor to terminate computations in a safe way. // Won't compile:…
resueman
  • 10,572
  • 10
  • 31
  • 45
12
votes
4 answers

Flowcharting functional programming languages

Flowcharting. This ancient old practice that's been in use for over 1000 years now, being forced upon us poor students, without any usefulness (or so do I think). It might work well with imperative, sequentially running languages, but what about my…
LukeN
  • 5,590
  • 1
  • 25
  • 33
12
votes
3 answers

Is there any difference between flatten and flatMap(identity)?

scala> List(List(1), List(2), List(3), List(4)) res18: List[List[Int]] = List(List(1), List(2), List(3), List(4)) scala> res18.flatten res19: List[Int] = List(1, 2, 3, 4) scala> res18.flatMap(identity) res20: List[Int] = List(1, 2, 3, 4) Is there…
user3335040
  • 649
  • 1
  • 7
  • 17
12
votes
2 answers

Scala generate unique pairs from list

Input : val list = List(1, 2, 3, 4) Desired output : Iterator((1, 2), (1, 3), (1, 4), (2, 3), (2, 4), (3, 4)) This code works : for (cur1 <- 0 until list.size; cur2 <- (cur1 + 1) until list.size) yield (list(cur1), list(cur2)) but it not seems…
Co_42
  • 1,169
  • 1
  • 10
  • 19
12
votes
1 answer

Closures and universal quantification

I've been trying to work out how to implement Church-encoded data types in Scala. It seems that it requires rank-n types since you would need a first-class const function of type forAll a. a -> (forAll b. b -> b). However, I was able to encode pairs…
Apocalisp
  • 34,834
  • 8
  • 106
  • 155
12
votes
9 answers

Functional Programming: Best Platform/Environment

I have been researching and playing with functional programming lately, solely to broaden my thinking about programming, because I find thinking "functionally" difficult. I have downloaded Glasgow Haskell and experimented with that. What I am…
Lawrence Dol
  • 63,018
  • 25
  • 139
  • 189
12
votes
1 answer

"Any function on finite lists that is defined by pairing the desired result with the argument list can always be redefined in terms of fold"

I was reading through the paper A tutorial on the universality and expressiveness of fold, and am stuck on the section about generating tuples. After showing of how the normal definition of dropWhile cannot be defined in terms of fold, an example…
Stoof
  • 687
  • 2
  • 6
  • 12
12
votes
9 answers

Is there a functional language for C++ ecosystem?

Java has Scala and .NET has F#. Both of these languages are very highly integrated into the respective Java and .NET platforms. Classes can be written in Scala then extended in Java for example. Does there exist an equivalent functional language…
pauldoo
  • 18,087
  • 20
  • 94
  • 116
12
votes
5 answers

Why should one prefer Option for error handling over exceptions in Scala?

So I'm learning functional Scala, and the book says exception breaks referential transparency, and thus Option should be used instead, like so: def pattern(s: String): Option[Pattern] = { try { Some(Pattern.compile(s)) } catch { case e:…
BasilTomato
  • 1,071
  • 1
  • 8
  • 14
12
votes
1 answer

nested recursive function in swift

I'm trying to do a nested recursive function but when I compile, the compiler crash (Segmentation fault). Here is my code : func test() { func inner(val : Int) { println("\(val)") if val > 0 { inner(val -…
Morniak
  • 958
  • 1
  • 12
  • 37
12
votes
3 answers

Query on Booleans in Lambda Calculus

This is the lambda calculus representation for the AND operator: lambda(m).lambda(n).lambda (a).lambda (b). m(n a b) b Can anyone help me in understanding this representation?
name_masked
  • 9,544
  • 41
  • 118
  • 172
12
votes
4 answers

Does Java 8 support functions as first class objects?

I've read today about the Java 8 release. But I don't understand fully the concept of reference methods in Java 8. Does this mean that Java now has the support of functions as first class objects? I have seen, how to construct a reference to…
SPIRiT_1984
  • 2,717
  • 3
  • 29
  • 46