[OPINION] - does language really matter if they alldothe samething?

Dietrich Epp dietrich at zdome.net
Mon Jan 26 01:04:07 EST 2004


On Jan 25, 2004, at 6:13 PM, Terry Reedy wrote:

> I assume that in your code, the same recursion base-casing is hidden 
> within
> the no-source-given choose-random-assoc macro, by how it rewrites the 
> text
> of its 'arguments'.  I suspect that if choose-random-assoc were an
> ordinarily lisp function, including in CL, with arguments evaluated 
> before
> the call, then random_sword_magic_power() would have to explicitly
> conditionalize its recursive call, just as in other languages.  If so, 
> then
> one very much does have to know that c-r-a is a macro in order to use 
> it
> efficiently and not write a redundant base-case check.

Yah, it is hidden, or as I would put it, abstracted away.  It's stuff I 
don't need to look at or know any more.  The 'and' solution wouldn't in 
the following:

(defun some-beverages ()
   (choose-random
     (5 '(water))
     (2 '(milk))
     (3 '(orange-juice))
     (5 (append (some-beverages) (some-beverages)))))

This is a bit closer, as I can't recall any recursive function I've 
written where the recursion can be excluded before we know which item 
'choose-random' selected.  Copying and pasting the above into clisp 
(after loading choose-random) yields results like:

(orange-juice)
(milk orange-juice)
(water water)

I strongly disagree with the statement that I need to know that 
'choose-random' is a macro.  In Python, I don't need to know that 
'lambda' creates a 'closure'.  All I need to know is how to use it.  I 
didn't give the source for 'choose-random' because I don't think it 
necessary, and besides, it's the first macro I ever wrote -- so it's 
poorly written (but I don't care as long as it works).  If I were to 
rewrite it, I would eliminate a level of parentheses.  By the way, I 
would NOT touch the parentheses issue with a ten-plus-one foot pole, 
ESPECIALLY on a Python list.

In Lisp, you could (and often do) use things that are macros and never 
know.  Such as the following:

(defun abs (x) (cond ((> x 0) x) (T (- x))))

Can you spot the macro?  There are anywhere from zero to two or more 
macros, depending on implementation.  'cond' will sometimes turn itself 
into a bunch of 'if' expressions (sometimes vice versa), and 'defun' 
often turns into a 'define' and a 'lambda', sometimes more (such as if 
the function has a docstring).  So if I wanted to use 'choose-random' 
would knowing that it's a macro help?  No.  What I need to know is that 
it takes pairs of probabilities and values and randomly evaluates one 
of the values.  A clarification probably says that it's safe to recurse 
inside 'choose-random'.  If I were new to Lisp, I would say, "Huh, 
cool.  Wonder how they did that?"  If I were experienced, I would 
think, "Oh, it's a macro."  But either way it's not really important.

P.S. In some flavors of Lisp, choose-random would be an ordinary 
function.  This includes a toy version that I wrote in Python, no less.

P.P.S. I thought of a paradigm that would be exceptionally painful to 
implement in Python.  It's a function found in some flavors of Lisp 
called 'arb'.  It returns its argument which causes the program to exit 
successfully, otherwise causes the program to exit unsuccessfully.  
Think about that.





More information about the Python-list mailing list