Live data from Hacker News

Ask HN: Why do functional programmers hate loops (for, while, etc.)?

news.ycombinator.com

1–10 of 73 posts

Re: Ask HN: Why do functional programmers hate loops (for, while, etc.)?

#4
To some extent for while loops are a matter of taste. But the issue is that unless you have mutable variables, you cannot use typical imperative control variables.

So 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.)?

#7
For some people, thinking in "functions" makes more sense than thinking in "algorithms".

The 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.)?

#8
post #6

Well, 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 ?

No, it isn't clearer. Just different. Which one is clearer depends who is reading and the way they think.

Re: Ask HN: Why do functional programmers hate loops (for, while, etc.)?

#10
Be very careful with "why do X hate Y?" Generally speaking, if someone comes out as outright hating Y, their opinion should be taken with a pound of salt.

Functional 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.

Post reply on HN