POV-Ray : Newsgroups : povray.off-topic : My first C++ program : Re: A test Server Time
1 Oct 2024 00:07:03 EDT (-0400)
  Re: A test  
From: Warp
Date: 22 Sep 2008 19:50:06
Message: <48d82f2e@news.povray.org>
Darren New <dne### [at] sanrrcom> wrote:
> OK. Question: I think I've figured out what some of your advice means...

> When you say "don't use new", are you really saying "use the calls to 
> new that are in the libraries, and you shouldn't have to do that 
> yourself"? Or are you really saying "C++ programs rarely need to execute 
> the 'new' operator"?

> The former I can understand. The latter would seem really confusing to 
> me, and I've been interpreting it as the latter, methinks.

  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.

  It is obvious that at some point a C++ programmer will have to deal
with 'new' and memory management. However, IMO that can be left for later.
First get to know the program without the dirty details.

  Of course using managed memory is a good advice for all C++ programmers,
beginners and experts. Even experts should avoid raw pointers and 'new',
unless there's a good reason not to. When you *must* use them, then it's
good to follow certain programming practices.

> >   One good thing in C++ is that usually you can *hide* all that overly
> > complicated and advanced code behind a simple and nice public interface,
> > so that the code which *uses* that code can be simple, straightforward
> > and easy to understand.

> Welll..... There's still a bunch of syntax. Template instantiation, << 
> and >> for I/O, etc.  I mean, compare Haskell add-up-a-list-of-numbers 
> to C++'s. Something like
>    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.
(And by the way, you advance an iterator with the ++ operator.)

  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

  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.

  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);

-- 
                                                          - Warp


Post a reply to this message

Copyright 2003-2023 Persistence of Vision Raytracer Pty. Ltd.