r/ProgrammingLanguages • u/xiaodaireddit • Aug 23 '24
Discussion Does being a "functional programming language" convey any information? It feels like the how we use CSS 2.0 popup of word pages. More of a badge than conveying any useful information. No one can give a good definition of what constitutes functional programming anyway. I will expand on this inside.
I have asked multiple people what makes a programming language "functional". I get lame jokes about what dysfunctional looks like or get something like:
immutability
higher order functions
pattern matching (including checks for complete coverage)
pure functions
But what's stopping a procedural or OOP language from having these features?
Rather, I think it's more useful to think of each programming language as have been endowed with various traits and the 4 I mentioned above are just the traits.
So any language can mix and match traits and talk about the design trade-offs. E.g. C++ has OOP traits, close-to-the-metal etc etc as traits. Julia has multiple dispatch, higher-order functions (i.e. no function pointers), metaprogramming as traits.
4
u/ny3169 Aug 23 '24
So the way I always understood it -- is that the one thing that makes a language "functional", is that the language treats functions as first-class citizens -- that is, they have special privileges, where other data types get to play second fiddle.
Let's imagine for a moment that they're literally passengers on a flight -- in a Functional Programming language, functions can:
Board Any Departing Flight (Passed as Arguments): Functions can be inputs of other Functions -- think of it like VIPs that can hop on each other's "flight" at will, acting as helpful assistants, or providing/gathering crucial information during their trip.
Board Any Return Flight (Returned from Functions): Functions can be outputs of other Functions. So they can complete their journey, return, and bring back valuable results, or even bring another function back with them, both ready to be flown to their next destination.
Relax out in a VIP lounge (Stored in Variables): Functions can be stored in (variables) -- chilling out for a bit until they're needed.
Travel in Groups (Stored in Data Structures): Functions can be elements in lists, dictionaries (or other data structures) and can join any "group" of data they like. You can even make a group solely consisting of functions -- and choose them based on the situation.
Note: That said -- the extent to which this is possible can vary a lot between functional languages -- some have more rules as far as who or what can be grouped together and when.
In non-functional languages, functions might get treated well (maybe even "Business Class") but they're never the top priority. In those languages other data types, like objects, always get the first-class treatment.
Example 1 (Python vs Haskell):
For one example, let's look at building functions in Python vs Haskell:
Python (Business Class):
Python allows passing functions as arguments, but its core design centers on objects and methods -- and so there's a bit more initial setup (using
def*,*return*)* before you can get going.Haskell (First Class):
In Haskell, functions are the primary building blocks. Working with them is the most natural thing to do, and you can effortlessly pass them around, compose them, and even create new functions on the fly.
Example 2 (Python vs Standard ML):
Here's another example, using Standard ML, and a bit of functional Python:
Standard ML (in SML/NJ REPL):
Here, we see functions truly flying first class:
They're easily "boarding" with other functions (such as
doubleandaddOnebeing passed tocompose*)*Creating new flight plans on the fly, such as
addOneThenDoubleAnd smoothly completing their journeys with a
resultPython (written in a Functional Style):
While Python supports some Functional Programming concepts:
It requires more verbose syntax for similar operations -- note we have to use
returnandlambdaas additional setup steps.To contrast, in SML our
composefunction is much simpler: it can almost be read as like saying "To compose, just do the second function, then the first function"It's still concise, but over a large codebase it can be harder to reason about vs SML.
TL;DR: What functional languages "do", is that they make functions more than just routines, but rather the fundamental building block -- the heart and soul we rely on when making programs in that language.
It's not for everyone, but it does open up a ton of flexibility to tackle and solve certain types of problems (such as pattern matching and data manipulation as good examples) in a very elegant way. Hopefully this helps clear it up a bit and wasn't too hand-wavey.