|
|
Warp wrote:
> It was not a generic advice for making C++ programs. It was an advice
> for a *complete beginner* who is starting to learn the basics of the
> language.
Oh, I see. Well, the handling of assignment and copy constructors is the
only bit that is particularly confusing to me, language-wise. I may not
be an expert, but conceptually that's the only concepts I don't think I
have down.
>> fold (+) my_list
>> to something like
>> for (j = std::list.begin(); j != std::list.end(); j = j.next())
>> i += j.value;
>> or some such. :-)
>
> At least the latter states more explicitly what you are doing.
Ehn. The call to fold does too, if you know its definition. Just like if
you don't know what std::list.begin() does, you're not going to
understand the C++. Basically, the Haskell line is "put plus signs
between the elements of my_list and evaluate the result."
> In the haskell case, it's not at all clear what you are doing. In fact,
> I don't even think that your haskell example is correct. I can't find the
> function "fold", but the closest thing I can find is the (rather unclearly
> named): http://www.zvon.org/other/haskell/Outputprelude/foldl1_f.html
Well, "fold" is the mathematical term. If you worry about associativity
and such, then you need foldl and foldr, and if you worry about what the
identity for the operator is, then you use the foldl1 and such. I guess
Haskell, unlike (say) Erlang, doesn't alias "fold" to one of those.
> It seems that, as the name clearly implies, the difference between
> foldl and foldl1 is that the first one applies the function to the
> second parameter and the first element of the list, then applies the
> function to the result and the second element of the list, and so on,
> while foldl1 there's only a function and a list and the first element
> of the list is now in the role of the second parameter to foldl.
Yep! That's basically it. But in functional languages, things like fold
and map and apply and such are used so often, they're as much an idiom as
for (i = 0; i < j; i++)
is in C.
> I think it would be perfectly possible to simulate the same thing in C++
> with something like:
>
> int result = foldl1(plus, intContainer);
>
> with proper implementations of the 'foldl1' and 'plus' templates. The
> foldl function would be equally easy:
>
> int result = foldl(plus, 64, intContainer);
Yep. I think it could very easily be a function in C++, or even C if you
want to specialize it every time.
--
Darren New / San Diego, CA, USA (PST)
Post a reply to this message
|
|