Profile photo for Mark Sheldon

Loops don’t make sense in a pure functional program.

A loop is a sequence of operations performed repeatedly until the program state has some desired property. while (n > 0) … or while (c != EOF) … The idea is that n will eventually be less than or equal to 0 or c will eventually be EOF. We use side effects to change the state of a program over time in the loop — the program state is mutable.

Since a pure functional language has no mutable state, such a construct makes no sense: continuing while the eternally unchanging value has some property is pointless.

This is a little more general than just mutable variables, though mutable variables are typical of imperative languages. You could have immutable variables but mutable references or other mutable structures, for example (as with ML).

Others have pointed out that there are programming techniques that allow you to think in terms of mutable state and loops in a pure functional language (using mapping and monads) if you can’t let go of that idea. There are problems for which changing state is a good model, of course. We do live in a stateful world with time, after all. But state, especially non-local state, makes programs hard to write, debug, and maintain.

View 11 other answers to this question
About · Careers · Privacy · Terms · Contact · Languages · Your Ad Choices · Press ·
© Quora, Inc. 2025