[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Accumulator




   Date: Fri, 24 May 2002 15:35:49 -0700
   From: Paul Prescod <paul@prescod.net>
   X-Accept-Language: en
   To: Guy Steele - Sun Microsystems Labs <Guy.Steele@sun.com>
   CC: ll1-discuss@ai.mit.edu
   Subject: Re: Accumulator
   X-Perlmx-Spam: Gauge=, Probability=0%, Report="INTERNAL_HOST"
   X-Filtered-By: PerlMx makes it fast and easy.  See 
http://www.ActiveState.com/Products/PerlMx/Header
   
   Guy Steele - Sun Microsystems Labs wrote:
   > 
   >...
   > Pattern 2c:
   > (setq x (loop for i from 2 to 20 sum i))
   
   I like that one. I don't quite understand how it works but it is pretty
   succinct and readable. But then if you've got a sum primitive why don't
   you just feed it the list of numbers from 2 to 20?
   
   sum(range(2,20))

In this context, "sum" is not a Lisp operation;
it is part of the syntax of the "loop" macro.

   > 
   > I conjecture that if Python allowed you to write
   > 
   > >>> x = reduce(+, range(2,20))
   > 
   > then this idiom would be somewhat more attractive.
   
   Python has an equivalent.
   
   >>> from operator import add
   <built-in function add>
   >>> x = reduce(add, range(2,20))
   
   Arguably it is the extra import statement that is turning people off. If
   so, I'm happy to see the use of reduce discouraged.
   
   There are only a few binary infix operators (compared to the number of
   things in the universe that are already functions). So you can't get by
   without the for-loop (or recursion or similar alternatives).
   
   Also, it is quite common in Python to make a function which accepts a
   list or n-ary parameters to start with, so the user doesn't have to do
   the loop. Come to think of it, operator.add COULD be implemented that
   way but isn't. It is strictly boolean.
   
By "boolean" I assume you mean "binary".

--Guy Steele