POV-Ray : Newsgroups : povray.off-topic : A comparison : Re: A comparison Server Time
10 Oct 2024 17:17:59 EDT (-0400)
  Re: A comparison  
From: Tim Attwood
Date: 18 Mar 2008 23:45:44
Message: <47e09a78$1@news.povray.org>
> Compare these two psuedocode snippets:
>
>   To compute SUM(LIST):
>     1. Let TOTAL = 0.
>     2. Let POSITION = first node of LIST.
>     3. Let TOTAL = TOTAL + value at POSITION.
>     4. Let POSITION = next node after POSITION.
>     5. If POSITION is not the end of the list, go to step 3.
>
> And now, the Other Way:
>
>   1. If LIST is the empty list then SUM(LIST) is defined as 0.
>   2. Otherwise, SUM(LIST) is defined as the first element of LIST + 
> SUM(rest of LIST).
>
> Personally, several things strike me here.
>
> 1. The first version seems a very long and wordy way of explaining 
> something that (to a human) is actually quite simple.
>
> 2. The second version is very much shorter. (And simpler?)
>
> 3. The second version looks like some kind of a riddle. Not a hard riddle, 
> but a somewhat baffling definition all the same.
>
> I think it neatly demonstrates the fundamental difference in the mental 
> model between imperative and functional programming though. Even a layman 
> who knows nothing about programming would (I suspect) appriciate that 
> these two methods are pretty different. (And without having to go into a 
> long lecture about how computers work, how to program in any specific 
> language, etc.)
>
> Anybody else have anything to add?

Neither of these ways is how you would do it if you weren't using a 
computer.
By hand you would add up the digits in the first column making carry marks,
and get the rightmost digit of the answer and work your way left, column by
column.  If there were several pages worth, you'd probably make sub-totals,
then add those up later. Those psuedocode examples are just describing
real code someone had already thought up, so it's not surprising that you 
can
tell which way they were thinking. Are you describing a process or an
algorithm?

1. divide the list into manageable sub-lists (pages)
2. add up the sub-lists and get sub-totals
3. add up the sub-totals and get the sum of the list

vrs

1. 0 for an empty list
2. number for a list with 1 number
3. number+number for a list with 2 numbers
4. sum of the first half of list + sum of the second half of list,
   for a list with at least 3 numbers.


Post a reply to this message

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