|
|
|
|
|
by chongli
1081 days ago
|
|
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 < sizeof(array_of_numbers); i++) array_of_numbers[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! |
|