r/functionalprogramming • u/YetAnohterOne11 • Jul 21 '23
Question What is the defining trait of functional programming?
Until not long ago I believed that the defining trait of functional programming is data immutability and lack of side effects. As in: 'Functional programming is a programming paradigm where all data is immutable, therefore guaranteeing referential transparency'.
In this way, I believed, methods/procedures/functions/subroutines/whatever turn into pure functions in the mathematical sense (a relation associating arguments to values such that for each argument there is exactly one value), hence the name 'functional programming'. This, FP proponents tout, allows us to employ the vast mathematical apparatus to design, analyze and understand codebases adhering to the principles of FP, which is FP's main advantage over imperative programming (where 'imperative' is defined as any programming that admits mutability and side effects).
However, this world view was recently shaken, as I've been repeatedly told from many sources that my understanding was not correct. Data immutability is neither necessary nor sufficient to make programming 'functional'. Indeed, I was told, the earliest functional languages such as Lisp arose even before people started placing emphasis on immutability, so Lisp doesn't even have the concept of immutability. On the other hand, data immutability is increasingly being emphasized in OOP world as well, and many OOP codebases, whether they mutate data or not, are hardly functional.
In light of this, what is the defining trait of FP? What, exactly, allows us to say that a code is 'functional'?
15
u/Gwaerondor Jul 21 '23
I've done a lot of programming in Erlang, and while it is immutable, purity is optional and side-effects are often desired (message passing and other things).
To me, the most important and paradigm-defining aspect is functions being first-class citizens and thus the existence of higher-order functions.
22
u/DogeGode Jul 21 '23
When using the word "functional" in a programming context, I usually point out that it has two distinct meanings:
The more a language – or a program(!) – exhibits and embraces those two properties, the more functional it is, generally speaking.
13
u/whitePestilence Jul 21 '23
Definitions like that are vacuous by nature. In a more general interpretation, I'd say that a style of programming is "functional" if it poses functions in the spotlight (as opposed to objects).
For example, while in OOP the focus is often on an object, i.e. a collection of data that mutates over time, a more functional approach might put the transformation the data can go through on the spotlight (which would be functions).
Immutability and higher order functions are traits Tha while not strictly necessary pair very well with functional pipelines.
About OOP using Immutability... That's just people drifting away from OOP. The mutability of state is at the core of the OOP paradigm (as understood by the industry). Using immutable data structure means using a more functional approach, in my opinion.
6
u/AWDDude Jul 22 '23
Oh oh I know this one… functions!
5
u/[deleted] Jul 21 '23
I think on a high level, with functional programming you’re more interested in declaring “what” you want to be done, whereas with imperative programming, you tend to focus more on defining the “how” of it (either by choice, necessity, or a mixture of both).
A good example is an imperative style for-loop that exposes a looping variable, the condition, etc. vs. a higher-order counterpart from FP that just takes, say, a collection and a function to apply to each element, and abstracts the rest away.
This, to me, is the fundamental point of FP as a paradigm, and all the typical qualities of FP languages are ultimately about serving this goal. We want to just be able to describe “what” to do without having to dive deep into what are often unnecessary or pro-forma procedural implementation details.
4
u/Puzzleheaded-Lab-635 Jul 22 '23
Referential transparency and immutability are the pillars in which all the other niceties of what is considered Functional Programming arise from. Don't let anyone else fool you into thinking otherwise.
3
u/delventhalz Jul 22 '23
In my mind, the key division between OOP and FP is that OOP tightly couples data and logic, while FP strictly separates data and logic.
OOP aims for domain-based encapsulation with their stateful objects and methods, while FP aims for structural simplicity with dumb data and (mostly) pure functions.
3
u/[deleted] Jul 23 '23
Functional for me means:
3
u/mvaldesdeleon Jul 24 '23
I'll add mine to the mix:
I think the key defining feature of Functional Programming, is that a Functional Program is all about evaluating expressions to produce some final result.
Contrast this with with a more traditional Imperative Program, where it's all about executing statements.
2
u/mckahz Jul 22 '23
It's not like any comp sci terms have fixed definitions, we're still figuring out how to program as a society and we haven't nailed down everything yet, so there's still a million different meanings for every word, and a million different words for every meaning. If someone says lisp is a functional language they're just saying that it has nice facilities for a functional style, but if you're reading a book about functional language compiler design then LISP wouldn't even qualify as an FP language. I would say that most of the time when people talk about FP they're talking about immutability and HOF, and to me a strong type system is also important and to another probability is.
I also don't think maths really has anything to do with why FP is good (although FP is good for maths), it's just that avoiding mutation means that the scope of what can influence a part of your program is entirely limited to the place (function) of interest. It also just makes it easier to think in a time independent way- I came from game Dev and the biggest hurdle to doing anything was just making sure my scripts were running in the right order. It sucked and is largely not an issue in an FP style.
2
u/grahamhutton Jul 24 '23
This lecture starts off with the question "what is functional programming?" https://youtu.be/rIprO6zoujM
3
u/pthierry Jul 21 '23
Lisp isn't a sound basis to define FP. Contrary to Scheme, Lisp wasn't based on lambda calculus (and it didn't have lexical scoping which Scheme introduced). Scheme's authors feel putting mutability everywhere was an error.
It's Indeed immutable data and pure functions that are the foundations of FP. Those are the feature that open up the specific advantages that FP provides.
See https://youtu.be/0if71HOyVjY
3
u/chandru89new Jul 21 '23
I've been reading "Can programming be liberated from von Neumann style?" (talk/paper by John Backus) and I think we find some of the first ideas of what a "functional programming" language looks like (and by that I don't mean the syntax/semantics or style but the theoretical foundations backed by a lot of math/algebra) there.
I've come to realize that what we call functional programming in everyday parlance is quite different from the original paper/ideas. Today, it's a mixture of lambda calculus, bits of OOP-ideas (eg typeclasses) and more.
The thing about data immutability is interesting. In the paper, Backus talks about the "state" of the program (calls it "history-sensitive") and how, for an FP language to be useful in the real world, it has to be "history-sensitive" (meaning, it has to store some data as a state, and also deal with state transitions - i.e, the stored data goes from one state to another ... mutation!). In the paper though he skirts this part (at least till what I've read of the paper) but acknowledges that work needs to be done towards this.
What I found very fascinating though was this idea of not having to assign variables at intermediary steps, having strong algebraic backing to be able to "prove" programs and their correctness which he tries to demonstrate as being hard or impossible to achieve in "von Neumann-style" languages. He uses the example of "inner product" and shows how you'd usually do it using a "for" loop, and then goes on to show how that's done in FP-style (no surprises here: point-free, works for all sizes of vectors whereas the "for" loop is limited, and is ultimately "provable" to be right.)
Personally, as I read through the paper (still reading), I find that ideas like data immutability, functions as values (and rather, almost everything being function), functional composition etc seem to just emerge/evolve out of this core idea that you could use algebraic ideas to craft a programming language.
link to the paper: https://dl.acm.org/doi/pdf/10.1145/359576.359579
1
u/oxijosef Jul 22 '23
Most if my functional programming experience comes from Clojure & OCaml. For me, the defining trait is composing functions to get something bigger. Stuff like immutability, category theory, separation of behavior and data, “how” vs. “what” and so on are more consequences or even specific fp styles. I consider Clojure as functional as OCaml, but the style is completely different.
0
u/[deleted] Jul 21 '23
[deleted] — view deleted comment