6 ms·
> For loops do not get the job done easily enough. There was a nice paper at this years POPL which (in my opinion) allows you to substantiate this claim. The
by fmap 9y ago
> For loops do not get the job done easily enough.
There was a nice paper at this years POPL which (in my opinion) allows you to substantiate this claim.
The paper is "Stream Fusion to Completeness", by Oleg Kiselyov, Aggelos Biboudis, Nick Palladinos, and Yannis Smaragdakis.
The actual question in the paper is how to compile away stream operations to imperative programs using for/while loops and temporary variables.
On the other hand, if we look at the output of the compiler we can see how complex it is to express natural stream programs using only for/while loops.
For instance, even very simple combinations of map/fold/filter create while loops and make it difficult to detect the loop trip count afterwards (in fact, you need an analysis just to detect that the loop terminates).
If you combine several such streams with zip, the resulting code makes a nice entry for obfuscated code contests.
Finally, if you use flatMap the resulting control flow doesn't fit into a for/while loop at all...
So for/while loops are not simpler than stream processing primitives and unless you explicitly introduce temporaries (i.e., inline the definition of map/fold/etc.) you quickly end up with very complicated control flow.
- skybrian 9y agoThis all depends on what "the job" being done well enough is. You can't use a research paper to refute the experience of the many programmers who successfully use for loops to get their work done. That's a statement about usability, not expressiveness. If you want to show something else is easier to use, you'd have to do a user study, and even that's not going to be universally applicable since it depends on the previous experiences of the user population being tested. It's why these things tends to be debated as a matter of taste.
- deleted 9y ago[deleted]