Live data from Hacker News

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

news.ycombinator.com

61–70 of 73 posts

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

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

But that can't be tail call optimized, so you should write something like

    sum(n, acc=0) = n == 0 ? acc : sum(n-1, acc+1)
To me that negates much of the 'declarative' character of a functional style. I don't feel it's really clearer than the imperative for-loop

    acc = 0
    for i = 1 to n
        acc += 1

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

#63
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

Looping was more natural to me, but learning Elixir (I've been using Learn Functional Programming with Elixir) made it so much easier to grasp and more natural. It's a fun language to use to boot.

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

#65
I think it has a lot more to do with readability. Certain conventions are easier to read. This is especially important as we realize that code is easier to read if we don't have to delve into the context.

For example, (I've spent a lot of time in C#,) let's say you have a collection of Foo objects, but you need to convert them to Bar objects. There's two general patterns in C# to do this:

  var bars = new List();
  foreach (var foo in foos)
  {
     bars.Add(foo.ConvertToBar());
  }

Versus:

   var bars = foos.Select(foo => foo.ConvertToBar());
Which one is more readable? If you're a newcomer to the language, or you generally haven't worked with "Select" before, the foreach approach might make more sense.

But, as you get used to "Select" and understand what it does, you'll probably come to the conclusion that foos.Select is easier to read, especially if you're looking at code that someone else wrote, where you might not have all of its context in your head.

Some other responses in this thread explain the difference by saying that "loops are saying how you want something done, functional is saying what you want done." This also is helpful when reading code where you don't have all the context in your head. (IE, code someone else wrote, or code you haven't worked with in a long time.)

To provide an example, you could have an array, int[] in C#:

  var sum = 0;
  foreach (var i in values)
  {
      sum = sum + 1;
  }

Or:

  var sum = values.Sum();
Both are valid C#. But, the functional approach is much easier to read, because you don't need to understand as much context when comparing the loop approach to using the Sum() method.

If you're a novice to C#, a novice programmer in general, or you're merely unfamiliar with Linq, the loop approach might seem easier. It's "not wrong..." But, remember, you're saying "how" instead of "what."

A codebase that constantly says "how" instead of "what" ultimately is harder to work with, because it takes longer for everyone to understand each others' code.

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

#66
It's not that they hate loops, it's that once you understand recursion and are able to implement it you never want to go back to loops again. Hearing that from others was the main reason I started learning FP, I wanted to see what the hype was all about and let me tell you it's absolutely true. Go learn recursion and get a handle on it and you'll see why.

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

#69
nobody hates anything. we don't hate while/for/etc loops.

We should just be careful when using them just like any other nested structure (if/else/while-do/switch/select/etc.)

Like if I see ``` for ... { for ... { if ... { } else { } // code here ... } ... } ```

That is objectively bad code and not readable.

Nobody hates anything. Most of us just dont like how people misuse them to write bad code because their managers pressure them.

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

#70

Earlier quoted context omitted.

forEach is still a loop

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.
Post reply on HN