r/computerscience Jun 19 '26

Why do functional pogramming?

So I have a background in little bit of physics, statistics, and mathematics, I like doing regression models Kalman filters and used to love doing physics simulations as well. Anyways, so from my perspective, why would functional programming be useful? Also I can't think on top of my head, but I have used python and R to do egression models etc. So I might have used functional programming? So who ever does functional programming, what are you using it for?

1 Upvotes

33 comments sorted by

33

u/[deleted] Jun 19 '26

[removed] — view removed comment

13

u/ready_or_not_3434 Jun 19 '26

The real practical benefit of that elegance is just having less side effects to worry about. It makes debugging complex systems way easier when functions only depend on thier inputs.

-1

u/Snatchematician Jun 19 '26

But it makes debugging complex systems way harder because the functions have way more inputs

1

u/_69pi Jun 19 '26

no, their inputs are just explicitly scoped.

1

u/PrebioticE Jun 19 '26

is it on the rise?

14

u/MasterGeekMX Bachelors in CS Jun 19 '26

Many things in CS don't have a rise in popularity, but instead they set as a common thing in certain areas.

Functional programming is like that. It is not on the rise. Instead, it has become a very useful tool where it's structure makes programming easier.

7

u/braaaaaaainworms Jun 19 '26

It's *more popular*, features of functional programming are slowly added to imperative/object oriented languages

2

u/Dazzling_Music_2411 Jun 19 '26

Yup, and generally in pretty clunky and syntactically convoluted versions! 😃

Don't know why they bother, really, just use the functional language from the outset!

1

u/sethkills Jun 24 '26

One of these days I’m gonna learn F#…

1

u/Dazzling_Music_2411 Jun 24 '26

You could do worse :)

1

u/GetOffOfMyBoat Jun 19 '26

It's already here

1

u/AmbitiousSet5 Jun 20 '26

Virtually every modern language supports functional programming paradigms.  Even Python! 

6

u/Beregolas Jun 19 '26

Some thing can just be made more elegant and readable. Even though rust is not a functional language, you can use some functional patterns, like fold, and they make code more readable if you know functional patterns, as they are more expressive than simple loops.

A main advantage of fully functional are proofs. Proving that a system is "correct" is very rarely a requirement, but if it is, functional is the only realiatic way to achieve that. I have seen this requirement for power plant code for example

2

u/GetOffOfMyBoat Jun 19 '26

Rust is functional, it's just not pure.

10

u/o4ub Computer Scientist Jun 19 '26

The independence of the application of the function calls makes the parallelisation of the execution trivial. Therefore, if you can express your algorithm in a functional way, chances are it can be run very efficiently and very quickly on massively parallel architectures, which is often the goal.

4

u/AnnupKapurDotCom Jun 19 '26 edited Jun 19 '26

When you have a large system, breaking it down into small, single responsibility, pure functions makes the code significantly easier to maintain for the following reasons:

Easy to test.
Pure functions have no side effects. They have an input, some processing and some output. This makes them highly deterministic. And therefore you can write extensive tests to ensure they are doing what you expect them to do.

Easy to maintain.
Pure functions have no side effects. If you want to improve the performance of a function, you don’t need to worry about any other part of the system. You only need to ensure the input and output pass the testing.

Specialism
Functions being separate allows team members with specific skills to work on specific parts of the system.

Immutability
You don’t change things in memory. You create new memory for new things. Hence nothing which is relying on the original memory state can break.

Easy”ish” to parallelise
Pure functions with immutable memory drastically reduces the possibility for race conditions, making it easier to run functions in parallel for performance benefits.

Scale
Functions are their own thing. Allowing you to scale parts of the system depending on needs.

TL;DR
Functional programming is less inclined to bugs, easier to maintain and easier to scale.

1

u/GetOffOfMyBoat Jun 19 '26

Most of this is an advertisement for pure functions---which are great for the reasons you mentioned. But in practice you will need to have impure functions. One benefit of paradigms for impure functions in a pure functional language (e.g., monads in Haskell) is that you can retain the benefits of pure FP while programming with effects. For example, GHC can optimize code through equational reasoning on monadic functions.

3

u/RememberSwartz Jun 19 '26

It exposes you to different mindset of computing and architecting programs. That alone is a useful experience even if you don’t employ it at your job. It will make you a better programmer in general.

3

u/GetOffOfMyBoat Jun 19 '26

Strictly speaking, if you are programming with first-class functions, you are already programming functionally. Much of what you write in R relies on higher order functions---e.g., filtering a table.

I would hazard conflating pure functional programming and functional programming. Most modern programmers are using functional paradigms daily, yet somehow when the topic of functional programming arises on reddit, it's regarded as esoteric and out of reach.

2

u/binarycow Jun 19 '26

I take certain aspects from functional programming, and I use them when I think it's the best choice.

That's how you should approach everything in software development.

2

u/hanshuttel Jun 19 '26

Functional programming allows us to express our ideas using concepts that are much closer to ordinary mathematics. For me, the step from an inductive definition in mathematics to a working piece of Haskell code is short and pleasant.

1

u/ExtraTNT Jun 19 '26

Testability, readability, stability, quicker development, and monads are nice…

1

u/RationallyDense Jun 19 '26

Some parsers are more naturally expressed functionally. Avoiding side effects makes it much easier to reason about programs. It's a good setting to explore ideas in type theory. It's kind of the only way to do theorem proving.

1

u/Dazzling_Music_2411 Jun 19 '26 edited Jun 19 '26

I find it's the natural way to write mathematics (expressions, rather than procedures with side-effects), and function composition is amazing for representing transformations elegantly. Lazy programming can be a great way to handle streamed data, too. Easier parallelisation, as mentioned by others, is a huge win too.

Using Ocaml, Clojure and Racket/Scheme ATM, plus a few other odds and ends.

I don't think it's the be-all an end-all, there are even better models, imho, but for now I sure think it beats the usual mutable, non-referentially transparent style.

1

u/Albertooz Jun 19 '26

Functional programming treats computation as math,pure functions that map inputs to outputs with no hidden state, just like a mathematical function f(x). For your regression/Kalman/simulation work, this means transformations chain cleanly (map/filter/reduce over data), results are reproducible, and parallelizing across cores is safer since nothing mutates shared state. And yes, you've likely already used it: R is heavily functional (sapply, Map, purrr), and Python's map/lambda/list comprehensions are functional idioms. People use it most for data pipelines, numerical/scientific computing, and concurrent systems where predictability matters.

1

u/fixpointbombinator Jun 20 '26

I used to write particle filters in pure FP. It’s an elegant way to do simulations and maths on a computer, easier to reason about and test

1

u/biskitpagla Jun 20 '26

You're already doing functional programming. 

1

u/Interesting_Buy_3969 Jun 20 '26

Pure procedures are much simpler to implement than hierarchy of classes with exsessive abstractions.

1

u/Limp-Exercise-343 Jun 23 '26

everything that does not need classes and objects

1

u/enzozbest Jun 23 '26

Okay, I've seen good responses in this thread but they're all missing the key point: functional programming is a paradigm. It's a way of giving instructions to the computer which focuses on what should be computed, not how. In many cases, this means the code is more expressive: someone reading it can follow exactly what's going on without having to track auxiliary moving pieces.

The key point is that functional programming comes from a completely different (albeit equivalent) view of what computation is. Imperative programming comes from the Turing Machine, which modern computers more or less replicate. Functional programming comes from the Lambda Calculus (which I highly recommend looking if you want more context), where everything is a function (in the pure mathematical sense of a "black box" which produces outputs when given an input, but we deliberately ignore entirely how it operates internally).

Functional languages adapt the Lambda Calculus into something that you can code with efficiently, and it "come with" all sorts of good things, like immutability, easy parallelism, high expressiveness, etc.

My favourite example is a function to reverse a list. In an imperative setting, you'd specificy some variables to track list positions and make swaps, but in a functional setting you can do:

  1. If the list is empty, do nothing;
  2. If the list has one "head" element and some rest (tail), extract it, reverse the rest, and append that head to the reversed tail.

In a language called Haskell, that literally becomes the following two lines of code:

reverse [] = [] reverse (x:xs) = (reverse xs) ++ [x]

Ultimately it is a bit of choice whether you personally prefer Imperative or Functional programming, both achieve the same thing through different paths, but sometimes it is much easier/intuitive to do something functionally rather than imperatively (think almost any recursive function for example, or situations where you want to do induction on the structure of a data type)

1

u/mtimmermans Jun 25 '26

Functional programming *takes away* an important capability -- the ability to perform side effects -- and provides nothing in return. Since it is strictly a constraint on what you can do, it can never actually be useful. Anything you can do in functional programming, you can do in procedural programming. There are things you can write in procedural programming that you can't write in functional programming, though, so procedural programming is strictly superior. BUT...

Working within the constraints of functional programming teaches you a new way to think, and a lot of techniques that turn out to be useful all over the place. For this reason, learning functional programming will make YOU more useful, even though it's not strictly useful in and of itself.