5 ms·
don't confuse standard libraries, libraries and languages :) Prelude is functional, C++ STL and the C library are imperative. There is a valid argument that a
by jheriko 12y ago
don't confuse standard libraries, libraries and languages :)
Prelude is functional, C++ STL and the C library are imperative.
There is a valid argument that a 'good ol' fashioned' bit of procedural code (for loop) makes this same algorithm easier to read than either of these (it certainly makes it shorter), since it tells you explicitly what it is doing rather than assuming (usually a safe assumption) that programmers can understand and trust things like sum, map and take/limit.
i'm thinking of something like:
int iTotal = 0;
for( int i = 0; i < 10; ++i )
{
iTotal += i * i;
}
- sanderjd 12y agoThat argument is not invalid, but the counter-argument is that the functional version does a much better job of capturing the intention of the code, rather than the procedure of it. The intention is to get the sum of the squares of the first 10 numbers counting from 1. My gut tells me that I have fewer bugs when I'm capturing intention rather than process, but I won't claim that it's definitely true. (Not to say this is an example of bug-due-to-process-over-intention, but you do have a bug – you're summing from 0 to 9 instead of 1 to 10.)
- deleted 12y ago[deleted]
- jheriko 12y agowhy is it not valid? these things are part of the libraries involved. as languages both Haskell and C++ are extremely free form to the point where you can abuse them into behaving like domain specific languages. or do you mean the bit about it being less code and easier to read? well done on catching the bug. :) this is my own fault for being sloppy and not paying attention.
- sanderjd 12y agoYou (understandably, due to the double-negative) misread! I said it is not invalid. Which was really a weaselly way of saying that it's valid but I don't really agree with it – sorry about that!