Live data from Hacker News

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

news.ycombinator.com

41–50 of 73 posts

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

#41
Functional languages use recursion instead of for and while loops. Recursion is a more general mechanism that can be used to implement the equivalent of for and while loops.

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

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

#42
The point is not about ignoring them; the point is about using a construction with higher semantics.

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

#43
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 think both are valid approach of understanding the problem. Recursion, on the other side, needs some basic math introduction.

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

#44
The rule of thumb I use is if something has side effects, I use a for loop. If it doesn't, I use whatever functional constructs the language gives me. They're obviously equivalent approaches, but the choice provides some semantics to people reading the code: look carefully at for loops for side effects.

What'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.)?

#45
post #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?

In von Neumann architectures, iteration will always be faster than recursion, because it'll need way less machine code. 2 caveats:

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

#46
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.

That is just untrue. Compilers tend to generate better code for imperative algorithms because current architectures implement a fundamentally imperative model. Yes, "sufficiently advanced" compilers can generate code that has similar performance to imperative algorithms from functional ones. But that requires some heavy lifting static analysis and optimisation passes. Have a look here: https://godbolt.org/z/sPT3snWE4. Only if you remove `#[inline(never)]` from `add1` will you get efficient code for the functional algorithm. This is a trivial example, but it ilustrates some of the nuances.

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

#47
post #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.

Functional languages support this type of operation in a few different ways.

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

#48
It's the fundamental difference between declarative and imperative programming.

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

#49

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…

I find concat pipelines to become unwieldy very quickly. I’ll generally turn to an imperative loop when my output doesn’t fit neatly into an FP paradigm, or requires a ridiculous amount of translations to accomplish. At the end of the day, I still want to write performant code.
Post reply on HN