So in short, functional programmers do not "hate" looping construct, but simply don't need them since you can use recursion instead (in practice you actually rarely use recursion directly, but instead use functions like map, fold, sum, etc. which are implemented recursively).
Ask HN: Why do functional programmers hate loops (for, while, etc.)?
41–50 of 73 posts
Re: Ask HN: Why do functional programmers hate loops (for, while, etc.)?
#42A loop is a label+goto in disguise. You have to look at what's inside to understand what the loop is doing; you can't get a global sense of how it is impacting data without looking at what it actually does.
In functional programming, loops are replaced by constructs such as foreach, reduce, map or filter, to indicate right away how the body of the loop operate on the data it is scanning. It enables a functional approach to looping, where one first thinks about how data are transformed -- about the functions to apply on items and what is the overall output of the loop.
Re: Ask HN: Why do functional programmers hate loops (for, while, etc.)?
#43Well, 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.)?
#44What's interesting is that you start to see less need for side effects in most cases. Still some, but a lot less than you'd think.
Re: Ask HN: Why do functional programmers hate loops (for, while, etc.)?
#45Loops 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?
1rst one: It really depends on your data structure. If you use a tree, recursion will be faster on most operations (unless you optimize like crazy, but tbh, it's never worth it). Even with simple linked lists, some operations are better written with recursion.
2nd: more often than not, using first order functions will be both more efficient and easier to read than a for loop. If you're using a foreach loop to apply a function to each items, please use a map.
Re: Ask HN: Why do functional programmers hate loops (for, while, etc.)?
#46Loops 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.
Re: Ask HN: Why do functional programmers hate loops (for, while, etc.)?
#47To 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.
One is through constructs like Clojure's `take-while`. Instead of setting a vairable when you want to exit the loop, you define a predicate that is false when the loop should exit.
Another is through `any` and `all` (aka `some` and `every`), which work in a similar way to `take-while`, except they reduce OR or AND across the returned values from the predicate as they go.
EDIT: grammar and clarity
Re: Ask HN: Why do functional programmers hate loops (for, while, etc.)?
#48Loops conflate two different questions: "what do you want?" and "how do you want it done?" If I write (Haskell):
map (+1) list_of_numbers
I have expressed only that I want one added to every number in a list. Whereas if I write (C): for (size_t i = 0; i
I have expressed both that I want one added to every number in the array and I have also described exactly how to do it, including the order I want it done in.So what's the big deal? On the one hand, the imperative approach gives me more control. On the other hand, I may not want that control! Perhaps I don't care what order the numbers are incremented in, I just want it done, and the extra details in the for loop are just "line noise" which gets in the way of being able to quickly read the code and understand what it does.
There are other factors, of course. One advantage to the functional approach is that the library author who supplies map (since it's not built-in to the language) is free to change the details under the covers. Perhaps they find a clever way to parallelize the implementation, then all of my code which uses map gets a free speed-up! Now, there may be other reasons why this won't work in practice (to do with legacy code, of course) but the principle is sound!
Re: Ask HN: Why do functional programmers hate loops (for, while, etc.)?
#49Be 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…
Re: Ask HN: Why do functional programmers hate loops (for, while, etc.)?
#50[1] At the very least, on variables outside of itself