Question

Functional programming is often explained to agree with lazy evaluation.

As far as I know lazy evaluation means that a method gets called if the evaluator/browser/etc. thinks that is the next best thing to do.

I've read about lazy evaluation here.

That makes me think; promises are designed to run, and after they are done you do the next best thing. You controll the flow.

I've read that promises are functional here.


In my tiny brain, those statements are the complete opposites.

The one is telling me to evaluate a statement whenever there is time; functional programming has no side-effects so it should not matter.

The other is telling me that you should run something when there is time, but you use Async behaviour after that. In other words, you make sure one function is done, then execute the next function.

Am I correct stating that pure functional programming should not contain promises? And that functions containing promises are by definition not pure?

I know promises and callbacks can be useful, just trying to give myself a clear view of the definition of functional programming.

Was it helpful?

Solution

I'm more interested in answering the questions within your post, or rather the source of your confusion.

As for the question in the title: are promises functional? yes they can be. See this page for promises in Haskell, which is the prototypical pure functional programming lanugage.

Your confusion as to the side-effecting nature of promises is well deserved. Check out the linked page above and take a look at the signatures of these functions. Notice the IO? That's Haskell's way of saying that the function may have side-effects.

Stating that "functional programming has no side-effects" is a bit harsh and technically incorrect. Imagine a programming language that is not allowed to access your harddisk, get input from the keyboard or print anything to the screen. We as users have a definitive need for side-effects at some point and functional programming only embraces purity of functions as far as possible.

As @Daenyth already pointed out, lazy evaluation and FP are different things. What is really confusing these days is that the term functional programming is used to mean a lot of these different things. Lazy evaluation is one of them, but purity, GADTs, monads, pattern matching, immutability and a lot of others are thrown into the mix as well.

Even the wikipedia article on FP is carefully not exactly defining what FP is. As far as I can tell, the only thing you cannot take away from functional programming, and still call it functional programming is the lambda calculus.

Licensed under: CC-BY-SA with attribution
scroll top