What are some of the best / worst functional programming issues?

I have often heard that functional programming solves many problems that are complex in procedural / imperative programming. But I also heard that it is not great in some other problems, that procedural programming is just fine.

Before I open my book on Haskell and dive into functional programming, I would like at least a basic idea of ​​what I really can use for it (outside of the examples from the book). So what is functional programming? What are the problems for which it is not suitable?

Update

I have good answers about this so far. I can't wait to start learning Haskell now - I just need to wait until I learn C :)

The reasons why functional programming is great:

  • Very concise and concise - he can express complex ideas with short, waterproof statements.
  • Easier to check than imperative languages ​​- well, when security in the system is critical.
  • The purity of the functions and the immutability of the data makes parallel programming more believable.
  • Well suited for scripting and compiler writing (I would appreciate that, though).
  • Mathematical problems are solved simply and beautifully.

Areas in which the struggle against functional programming is:

  • Debatable: web applications (although I think it will be application dependent).
  • Desktop applications (although it depends on the language, F # will probably be good at that, right?).
  • Everything critical to performance, such as game engines.
  • Everything related to a large number of program states.
+42
functional-programming haskell
Jun 15 '09 at 21:43
source share
11 answers

Functional programming is superior in compression due to the existence of higher-level functions (map, lfold, grep) and type inference .

It is also great for general programming for the same reasons and that further enhances the ability to express complex ideas in a concise statement without confusion.

I appreciate these properties because they make interactive programming believable. (e.g. R , SML ).

I suspect that functional programming can also more easily verify that other programming approaches are beneficial in critical safety systems (nuclear power plants and medical devices).

+13
Jun 15 '09 at 21:49
source share

I would say that functional programming is suitable for solving problems, for example. AI problems, math problems (it's too easy), a game engine, but not too good for developing a graphical user interface and user controls or a desktop application that requires a trendy interface. I find it intuitive to think as follows (although this may be too much generalization):

Back-end Front-end Low-level C C++ High-level FP VB, C# 
+11
Jun 18 '09 at 10:33
source share

It cannot be directly tied to functional programming, but nothing compares alliances in the design and implementation of data structures. Let's compare two equivalent code snippets:

F #:

 type 'a stack = Cons of' a * stack |  Nil
 let rec to_seq = function
     |  Nil -> Seq.empty;
     |  Cons (hd, tl) -> seq {yield hd;  yield!  to_seq tl}
 let rec append xy =
     match x with
     |  Nil -> y
     |  Cons (hd, tl) -> Cons (hd, append tl y)
 let x = Cons (1, Cons (2, Cons (3, Cons (4, Nil))))
 let y = Cons (5, Cons (6, Cons (7, Cons (8, Nil))))
 let z = append xy
 to_seq z |> Seq.iter (fun x -> printfn "% i" x)

Java:

 interface IStack<T> extends Iterable<T> { IStack<T> Push(T value); IStack<T> Pop(); T Peek(); boolean IsEmpty(); } final class EmptyStack<T> implements IStack<T> { public boolean IsEmpty() { return true; } public IStack<T> Push(T value) { return Stack.cons(value, this); } public IStack<T> Pop() { throw new Error("Empty Stack"); } public T Peek() { throw new Error("Empty Stack"); } public java.util.Iterator<T> iterator() { return new java.util.Iterator<T>() { public boolean hasNext() { return false; } public T next() { return null; } public void remove() { } }; } } final class Stack<T> implements IStack<T> { public static <T> IStack<T> cons(T hd, IStack<T> tl) { return new Stack<T>(hd, tl); } public static <T> IStack<T> append(IStack<T> x, IStack<T> y) { return x.IsEmpty() ? y : new Stack(x.Peek(), append(x.Pop(), y)); } private final T hd; private final IStack<T> tl; private Stack(T hd, IStack<T> tl) { this.hd = hd; this.tl = tl; } public IStack<T> Push(T value) { return new <T> Stack(value, this); } public IStack<T> Pop() { return this.tl; } public T Peek() { return this.hd; } public boolean IsEmpty() { return false; } public java.util.Iterator<T> iterator() { final IStack<T> outer = this; return new java.util.Iterator<T>() { private IStack<T> current = outer; public boolean hasNext() { return !current.IsEmpty(); } public T next() { T hd = current.Peek(); current = current.Pop(); return hd; } public void remove() { } }; } } public class Main { public static void main(String[] args) { IStack<Integer> x = Stack.cons(1, Stack.cons(2, Stack.cons(3, Stack.cons(4, new EmptyStack())))); IStack<Integer> y = Stack.cons(5, Stack.cons(6, Stack.cons(7, Stack.cons(8, new EmptyStack())))); IStack<Integer> z = Stack.append(x, y); for (Integer num : z) { System.out.printf("%s ", num); } } } 
+6
Jun 16 '09 at 20:03
source share

Functional programming would be good for concurrent programming. The fact that you do not rely on state changes with functional programming means that different processors / cores will not step on each other. Therefore, types of algorithms that are well suited for parallelism, such as compression, graphical effects, and some complex math problems, are also usually good candidates for functional programming. And the fact that multi-core processors and GPUs are growing only in popularity also means that the demand for this type of thing will grow.

+5
Jun 15 '09 at 22:08
source share

Some problems I found functional programming suitable for:

  • concurrency
  • Compilers
  • scripts

Problems that I personally consider not very suitable:

  • web applications (but maybe only me, Hacker News, for example, implemented in LISP)
  • desktop applications
  • game engines
  • things in which you go through many states.
+4
Jun 15 '09 at 21:52
source share

I find that Haskell is well suited to do something mathematical. Not that it was a real professional project, but I made a poker solver and analyzer with it. Having a mathematically provable program is wonderful.

As far as it doesn't fit, this is something where performance is a priority. You have less control over the algorithms used, since it is more declarative than imperative.

+4
Jun 15 '09 at 10:17
source share

I do not agree that FP cannot be used for web applications. I know that Paul Grahm and Robert Morris launched Viaweb, which used Lisp to deliver web applications. I think that when you get closer to hardware (device drivers, the kernel, etc.), you want to use it as low-level as possible. This is because if more abstractions are used, then it is more difficult to debug in case of errors. Take a look at the article “The Law of Feverish Abstraction” by Joel Spolsky.

+4
Jun 16 '09 at 20:21
source share

In fact, I like to use pure functional code for problems that require managing a large number of states. These sibling languages ​​typically provide the best mechanisms for explicit state handling, since they do not allow it to be done implicitly. Implicitly managing the state may seem easier on a small scale, but as soon as the state starts to become complex, you run into problems without the correct guarantees that you get from running this FP method.

+3
Jun 19 '09 at 7:41
source share

I believe that the simplicity of functional programming for mathematical problems with matrix mathematics is absolutely beautiful, take a look at how these types of problems are solved in the Scheme!

+2
Jun 15 '09 at 22:09
source share

I would say that functional programming will have problems for low-level , operating system kernels, device drivers, etc.

I said: “I have problems” and not “cannot be used” (due to Turing's equivalence, everything can be used for anything).

An interesting question is whether this problem is fundamental in functional programming (since physical devices have a state) or if we can imagine a system-oriented language / functional programming environment. For example, BitC is only partially functional (it is highly dependent on volatility).

+2
Jun 18 '09 at 11:15
source share

Functional and object-oriented paradigm have the power of orthogonality. You can say that functional programming is especially important for verbs and object-oriented programming of nouns. Or, more practical: Object orientation makes adding new data simple, and functional programming makes adding new tools easier. Both require code changes to achieve a different goal. Would you like to read http://citeseerx.ist.psu.edu/viewdoc/download?doi=10.1.1.18.4525&rep=rep1&type=pdf (Synthesizing project-oriented and functional design to facilitate reuse

+1
Jun 11 2018-11-11T00:
source share



All Articles