Ask HN: Why do functional programmers hate loops (for, while, etc.)?
1–10 of 73 posts
Re: Ask HN: Why do functional programmers hate loops (for, while, etc.)?
#2Re: Ask HN: Why do functional programmers hate loops (for, while, etc.)?
#3Re: Ask HN: Why do functional programmers hate loops (for, while, etc.)?
#4So something like:
bool done = false;
while (!done) {
...
}
makes no sense unless you have some way of setting `done = true`; which is not possible in purely functional languages like Haskell.Re: Ask HN: Why do functional programmers hate loops (for, while, etc.)?
#5Re: Ask HN: Why do functional programmers hate loops (for, while, etc.)?
#6Instead of looping from 1 to 5000, you define the relationship instead:
sum(1,n) = 1 + n + sum(2, n-1).
Isn't this clearer to understand problem first, instead of just looping ?
Re: Ask HN: Why do functional programmers hate loops (for, while, etc.)?
#7The former prefers a "declaration" of THE state. for/while loops imply a change of state, so they're a big no here.
The latter prefers an "imperative set of steps" to go from initial state to the desired state. for/while loops make the "steps" easier here.
Re: Ask HN: Why do functional programmers hate loops (for, while, etc.)?
#8Well, how to calculate sum from 1 to 5000 ? Instead of looping from 1 to 5000, you define the relationship instead: sum(1,n) = 1 + n + sum(2, n-1). Isn't this clearer to understand problem first, instead of just looping ?
Re: Ask HN: Why do functional programmers hate loops (for, while, etc.)?
#9Re: Ask HN: Why do functional programmers hate loops (for, while, etc.)?
#10Functional programming is fundamentally at odds with imperative constructs. It's not that people hate them, it's that they generally just don't fit the paradigm of "build a pipeline of steps (functions) and feed data into it" as well. Most of my software is functional, but you will find the occasional C-style loop, especially if the map/filter/reduce alternative is ridiculously complicated to read.