Live data from Hacker News

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

news.ycombinator.com

71–73 of 73 posts

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

#71
post #18
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 ?

Well, how to calculate sum from 1 to 5000 ? Instead of looping from 1 to 5000, you compute (1 + 5000) * 5000 / 2 This is what "understand problem first" actually means

But now you can run into problems with integer overflow. It can be solved:

    sum(n) = is_even(n) ? n/2 * (n+1) : (n+1)/2 * n
because of one n and n+1 will always be even. But it show that unfortunately things are often not as straightforward as they first appear.

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

#72

Earlier quoted context omitted.

The functional programmers (in my circle) have latched on to forEach/map/reduce/filter etc as the bibile of functional programming. Writing a simple for..each loop will make them reject PRs

Sure, but `forEach` is not even in the same category as map/filter/reduce. `forEach` is in no way functional.

I mean just search for "is forEach functional programming"

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

#73

Earlier quoted context omitted.

Sure, but `forEach` is not even in the same category as map/filter/reduce. `forEach` is in no way functional.

I mean just search for "is forEach functional programming"

forEach is just an alternate syntax for a for..in loop, that instead of being a block it takes a closure. It's not functional, since it's only used for side-effects.
Post reply on HN