No evidence that using map instead of loops "frees up brainpower."
No evidence that using map instead of loops "frees up brainpower."
It saves brainpower because you you don't need to analyse the logic within the structure to understand what the operation is intended to produce.
E.g. in the old days:
10 I=1
20 PRINT A[I]
30 I=I+1
40 IF I <= 10 THEN GOTO 20
You need to parse the logic to realise "Ah, this is a for loop". The I variable and the IF statement are bookkeeping code that doesn't have any relevance to the actual effect you're trying to produce.This construct is common enough that the for-loop was developed. Likewise with the if/else construct and others.
The old fogeys would say "we don't need this structured programming nonsense, gotos are more flexible. I can easily look at this and understand what's going on".
Structured programming is now widespread enough that we don't think of it as anything special, it's just part of "programming" in general.
Now there are several common operations that we do when looping over a list of items. Things like maps, folds and gathers are just building another level on top of structured programming, hiding the bookkeeping code and making similar operation similar, so you can concentrate on the unique aspects of the program.
You're not used to maps, folds, and filters (the bulk of sequence comprehension), so they don't free up your brain power. But seriously, they're not complicated, and using them instead of regular loops often reduce the amount of code by a factor of 3 to 5. Simple code, where the same idioms come back over and over —just like in regular loops, only shorter.
Don't tell me that doesn't free brainpower, eventually.
Fortran was cryptic in the same sense: you had to perform even more complex translation to get to the "real" assembly.
In other words, you're doing it wrong. You think in sequential terms, loops… and are translating list comprehension to that. Just don't. There are more direct ways. In a list comprehension, there's generally a filter part, and a map part. While they are interleaved during the execution, they are logically separate, and you should think of them that way.
I once specified the processing of a sequence of data as a pipe of maps and filters. A colleague came to me and told "how can you waste all that memory, generating all those lists around?". Dude, this is a specification of the end result, not a computational model. Of course I won't generate 10 big arrays.
Simply put, you need to separate the "what" from the "how".
A loop is way easier to debug than a filter or a map, you just step through it. Who cares about code length? Haskell doesn't become a usable language until someone comes up with a decent dataflow debugger, if that's even possible.
That one needs to be framed on a wall.
I'm definitely in the first camp (think to program), and have no problem with paranoid type systems, and I seldom need a debugger. Now I understand why other people do.
The mantra "well typed programs can't go wrong" actually means "our well typed programs can't go wrong, or we are so screwed."
Why can't you just step through the function being called by map/filter? You set a breakpoint on the function and examine the values when it is called.
Someday, someone might discover how to do good dataflow debuggers that could work well for time-intensive (read: stateful) programs, and in that case, I would be happy to indirect my control flow a lot more than I do. But no one has figured out how to really do that yet.