One of the benefits of functional programming is that it's much simpler to figure out how your code will run. Because functional languages are based on math, you're really definining functions in terms of equality. As such, you can substitute terms for one another. In this example, I'll use Haskell, because the syntax highlights this point, but the principle applies in multiple languages (Erlang, Elixir, OCaml, etc).
In Haskell, when you write x = 5
that means that x
and 5
are the same thing. As such, whereever you have an x
in your program, you can replace it with 5
, or vice-versa. Because x
and 5
are the same, these substitutions don't break your program. This ability to substitute terms for one another is called equational reasoning.
In fact, this is how Haskell runs your program — it repeatedly applies substitutions until it has one giant main
function.
I'm going to work through a short example using linked lists.