Lies They’ve Been Telling You About Functional Programming
Tags: concept, functional
Published: 2020-08-13 02:03:08

[datalog]: http://www.ccs.neu.edu/home/ramsdell/tools/datalog/datalog.html
[elm]: https://elm-lang.org/
[erlang]: https://www.erlang.org/
[formal-logic]: https://www.fecundity.com/logic/
[ghc]: https://www.haskell.org/ghc/
[haskell]: https://www.haskell.org/
[jvm]: https://www.java.com/
[julia]: https://julialang.org/
[kodumaro]: https://kodumaro.cacilhas.info/2019/09/lies-they-told-you.html
[lambda]: http://www.cse.chalmers.se/research/group/logic/TypesSS05/Extra/geuvers.pdf
[logic-programming]: https://www.britannica.com/technology/logic-programming-language
[monad]: https://wiki.haskell.org/All_About_Monads
[monoid]: https://encyclopediaofmath.org/index.php?title=Monoid
[mlton]: http://mlton.org/
[oop]: https://www.webopedia.com/TERM/O/object_oriented_programming_OOP.html
[prolog]: https://www.swi-prolog.org/
[python]: https://www.python.org/
[ruby]: https://www.ruby-lang.org/
[scala]: https://www.scala-lang.org/
[scala-dotnet]: https://www.scala-lang.org/old/node/10299
[scala-js]: https://www.scala-js.org/
[scala-native]: http://scala-native.org/
[sml]: http://sml-family.org/
[sql]: https://standards.iso.org/ittf/PubliclyAvailableStandards/c053681_ISO_IEC_9075-1_2011.zip
[Y-combinator]: https://en.wikipedia.org/wiki/Fixed-point_combinator

Post from [Kodumaro][kodumaro].

**Don’t believe in programming gurus!** Said this, let’s go to some definitions, and then, take a look at the lies they’ve been telling.


![λ (*lambda*)](https://dev-to-uploads.s3.amazonaws.com/i/ivle94v49kuif2g9am31.png)

**Functional programming** aims to implement the [λ-calculus][lambda] as a full programming environment.

Functional programming (and λ-calculus) is **not** about using functions, neither about quitting [object orientation][oop].

λ-calculus is about first-class and higher-order functions, and everything as a function. Of course “everything as a function” is a bit dramatic for the programming context (but not impossible), however every other *lambda* feature is reasonable to be called functional programming.

First-class function is the one which can be used as parameter or set to a slot. First-class as in “first-class citizen”, with all rights.

Higher-order function is that which takes another as parameter, or may return one.

Besides, there are some constraints that define the functional paradigm:

- Pure functions: functions **cannot** have side effects.
- Recursion: a function might call itself, loading a new stack.
- Tail-call optimisation: the last function call inside a function must replace the current stack. It prevents stack overflow when going into recursions.
- Determinism: the functions must be **idempotent**, that means, given the same parameters, you shall get the same return, no matter how many times it’s called.
- Immutability: no value can be changed. Once set it’s for good.
- [Monoid abstraction][monoid]: support for type wrappers that manage a binary operation over the wrapped type.

And there are some optional features:

- Non-strictness: a function can be strict or lazily evaluated. On lazy evaluation, functions are named “non-strict” and performed on demand only the strictly necessary parts.
- Typed *lambda*: functions (and values) can be typed by their structure or signature.
- Free and bound variables: traditionally functions may contain bound (existent only inside the stack) and free variables (got from parent context). Alternatively a functional system may forbid free variables, allowing only bound ones. Then you’ll need a [fixpoint combinator][Y-combinator] for recursion.
- [Monads][monad]: structures to build up computation sequences by combining sequential building blocks.

Now that we’ve made those definitions clear, let’s talk about the misconceptions spread through the programmer’s communities – mostly by their very gurus.

### (Ruby|Python|Julia) is a functional programming (⁉️)

You’ve probably heard it.

[Ruby][ruby] **isn’t** a functional programming, just as [Python][python] is not. Those are object-oriented imperative languages with **one or another** functional feature, but not enough to be considered even impure functional.

[Julia][julia] is an impure functional language indeed, but with a lot of imperative features that disfigure its functional trait.

### Functional programming is about using functions (⁉️)

When a guru says that, you can be sure he’s bullshit.

We’ve seen above what functional programming is about.

### Erlang is a logic programming language (⁉️)

[Logic programming][logic-programming] is to [formal logic][formal-logic] as the functional programming to λ-calculus.

The logic programming concepts are quite different from functional programming, and deserve a whole dedicated post about. Briefly, it’s about describing the facts’ domain and querying it.

Examples of logic programming languages are [Prolog][prolog], [Datalog][datalog], and [SQL][sql].

[Erlang][erlang]’s syntax is strongly inspired on Prolog’s, but even yet functional. Erlang sticks to all functional constraints and uses some logic features to provide some typing, which is a functional feature too – typed *lambda*, you saw it just above.

Furthermore, Erlang uses the same Prolog’s syntax for defining facts to declare function signatures – not to define facts.

So Erlang **isn’t** a logic programming language, but a **fully** functional programming one instead.

-----

There’s so much more about functional programming, and you should get involved. I recommend taking a look at [Haskell][haskell] for fully functional programming (install [GHC][ghc]), [Standard ML][sml] for impure functional programming (install [MLton][mlton]), [Elm][elm] for front-end functional applet, and [Scala][scala] for functional [object orientation][oop] over [JVM][jvm] (or [.NET][scala-dotnet], or [Node.js][scala-js], or [natively][scala-native]).