4 ms·
As someone who only discovered functional programming this year (and now do almost all my programming in OCaml), I would say that the biggest revelation that I
by crm416 13y ago
As someone who only discovered functional programming this year (and now do almost all my programming in OCaml), I would say that the biggest revelation that I came to (and didn't really appreciate until I'd studied the subject for a while) was the lack of mutability in (pure) functional programming. This allows you to maintain invariants and makes it much easier to reason about your code, which has huge advantages when you're working with concurrency or parallelism.
Here's one of my favorite examples. Say we have a set S and an element x, as in the following code:
"let S = Set.empty () in
let S = Set.add S x in
f ();
if (Set.mem S x) then..."
If our code is purely functional, then it's impossible that x is no longer in S after our call "f ()". However, if we're using mutability, then maybe in our function f, we remove x from S. If that's the case, we really can't be sure what will happen when we reach our if-statement.
- tikhonj 13y agoThis is certainly one of my favorite properties of functional programming: there is simply less to worry about. Everything is either local or explicit--distant parts of my code cannot implicitly affect each other. However, for me, the real realization was this combined with the fact that these languages also manage to be more expressive than the imperative ones I was using before. This might seem a bit counter-intuitive--after all, it feels like they give you less power, not more--but it has been the main draw to FP for me. So the code is simpler, there is less to worry about and it's more expressive. What not to like?