[fpc-pascal] More Filter Madness
juergen.hestermann at gmx.de
Sat Oct 15 12:02:15 CEST 2011
Rich Saunders schrieb:
> On 10/14/11 2:39 AM, Nataraj S Narayan wrote:
>> Good candidate for recursive algorithm i think.
> At first glance maybe, but it is actually a horrible candidate. That is
> due to the fact that the number of values is unknown and recursion has a
> built-in limit based on the stack size. So if your logic is recursive it
> will work one day on one file of numbers and not the next on a longer
Exactly! I often see advices to use recursive function/procedure calls
because it looks so elegant but actually it can crash quite easily
(depending on stack size and local variables). Most people don't think
about this and believe that if it works on a test it will always work.
But that's delusory.
IMO recursive function calls should only be used if there is a known
limitation of recursion levels and if that maximum will safely work
under all circumstances.
> Much better to simply maintain a summed value, read a value, add it to
> the sum, write the current sum, and continue until done.
Yes, in this case it is much better and also much easier.
More information about the fpc-pascal