Live data from Hacker News

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

news.ycombinator.com

31–40 of 73 posts

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

#31
I think one problem is that some of the syntax doesn't express what you are trying to do, it's just low level mechanism, especially C for loops for example.

But going further, many loops are actually maps, where you do something independently with each element of a list. Why formulate that as a linear visit to each element?

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

#32

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.

Yeah for sure! The tricky, prickly part of talking about FP is that we get into semantics a lot (eg. I'm sure there's Haskell experts who wouldn't consider JavaScript's `Array.prototype` functions to count as FP). Generally I'm thinking about `foo.map` vs. `for n in foo...`. Maybe "imperative" is a bit of a loaded term for this point.

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

#33
post #5

functional programmers are kind of like cargo bike riders. they know they can solve their problem otherwise and in a much simpler fashion but insist on riding the cargo bike.

How much further could you take this analogy?

Cargo bikes don't get stuck in traffic; functional programmers don't like getting stuck debugging.

Cargo bikes are easy to park; functional programs are easy to run in a REPL.

...and so on.

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

#35
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 can see why I never got very far with functional programming, it's much too clever for me. I don't see how this example has any sort of limit test in it at all much less stops at or rather defines an extent of 5,000. oh well imperative peasant life can be a drudge but it gets my corn sown one kernal at a time.

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

#36
1. Immutability: Functions are expressions that return a singular value without mutating state. Most loops are not expressions and have some mutation happening.

2. Composition: If the loops don't mutate there are functional alternatives (map, reduce/fold, filter, etc) which are composable.

3. Abstraction: Loops are traversal instruments. Sophisticated functional languages / environments have some mechanism for generalizing traversal of any data structure into a higher order function or some form of reusable generalization.

That said, I use a lot of loops in library code for performance and expose functional interfaces for consumers.

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

#37
Not all FP programmers hate them, I use them in a pinch.

It's about what conveys the intent most clearly.

What do you prefer?

``` let sum = 0; for (let elem of array) { sum += elem.value } return sum ```

Or

``` Math.sum(...array.map(elem => elem.value)) ```

What about

``` let parent = document.createElement("div") for (let user of users) { let userElem = renderUser(user) parent.appendChild(userElem) } return parent ```

vs

``` let parent = document.createElement("div") parent.appendChild(...users.map(renderUser)) ``` vs ``` {users.map(user => )} ```

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

#38
post #5

functional programmers are kind of like cargo bike riders. they know they can solve their problem otherwise and in a much simpler fashion but insist on riding the cargo bike.

I love this comment because I like to use functional programming at times and I mostly ride a cargo bike. You came right after me with this one.

Nothing beats fitting a week of groceries on your bike, though.

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

#40
It's kind of silly IMHO. Recursion has plenty of "state", it's added on the stack before each function call. So under the hood it's all stateful. But instead of e.g. a nice simple counter variable, you've got an ever growing pile of stack frames - with hopefully some compiler shenanigans to eliminate them and convert it to a counter internally.
Post reply on HN