Live data from Hacker News

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

news.ycombinator.com

21–30 of 73 posts

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

#21
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 ?

do people really think this is clearer than looping.. my mind immediately goes to a loop here

some people think in "what" and some people think in "how".

some people think in both. they should write blogs and help the whats and the hows communicate.

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

#22
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 ?

I would argue

  sum(0) = 0
  when n != 0: sum(n) = n + sum(n-1) 
is kind of clearer. But quite often it is not that easy to give an recursive solution for an iterative problem. When you want to change every even number in an array/list from n to n+1 you can somehow do it recursively, but it is mor obvious to iterate over the elements (which in many functional languages can be expressed by "apply" or some similar function).

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

#23
post #14

Loops have the potential to be very inefficient. As an exercise you can write two scripts in Javascript, one that has a for loop and another that does the same thing functionally. Then you can use Node to dump the VM instructions. You might be surprised at how many fewer instructions the functional version requires.

You have a cite or link exploring this?

I had thought tail-call optimization was a way to get inefficient recursion to parity with iteration, by turning it into iteration. I'd never heard of recursion being faster on its own.

Is it a javascript-specific thing due to how their int types work?

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

#24
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 ?

If i ask you to add from 1 to 10 , is that how you add ? 1 + 10 + let_me_add_from2_9 ? So yeah, not at all natural.

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

#26

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 functio…

>"build a pipeline and feed data into it." Arrows in Haskell are actually pretty good at building functional left to right data processing pipelines.

Arrows [1] are great, not sure why you were getting downvoted on that.

---

[1] https://en.wikibooks.org/wiki/Haskell/Understanding_arrows

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

#28
most functional techniques wrap walking a loop into a few functions, like fold. writing loop constructs can be error prone. one is much less liable to make a mistake passing in an array into a function and have it processed using another function. idea being separation of logic. why care about loops when one only thinks in arrays and streams being processed? i don't think i've written a for loop in js in a couple years at this point with the help of libraries like ramda.

oh and loops constructs are not composable...

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

#29
In theory, purely functional programming is declarative in nature - there are things you can tell about the state of the program just from reading the code, whereas in imperative programming (with loops and changing state) you can't tell with confidence what the state of the program will be without running it. There's a certain appeal to the declarative flavour that I used to care a lot about and somehow managed to forget to care about. FWIW I can't say that my programs became worse when I forgot to care about purity. But there may be some scenarios and domains where this sort of confidence can make a difference, and I just happen to not work often in these areas.

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

#30
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 ?

I don’t think that’s much clearer at all. I realize that you’re citing a specific example, but if your approach is to understand the problem first, then you may as well use the closed-form solution of your problem:

sum(1,n) = n*(n+1)/2

Which some compilers will produce for you, even if you use loops.

Maybe if you chose a different example, like generating power sets or the Fibonacci sequence, your point would be better made?

Post reply on HN