[OPINION] - does language really matter if they all do the samething?

Terry Reedy tjreedy at udel.edu
Mon Feb 16 18:45:09 CET 2004


"Paul Rubin" <http://phr.cx@NOSPAM.invalid> wrote in message
news:7xznbj8lwb.fsf at ruckus.brouhaha.com...
> Dietrich Epp <dietrich at zdome.net> writes:
> > My Lisp project related to an RPG and random generation of items.
> > It had lots of code like the following:
> >
> > (defun random-sword-magic-power (quality)
> >    (choose-random-assoc
> >      quality (poor medium good)
> >      ((5 0 0) (glows-in-the-dark))
> >      ((3 3 0) (magically-silent))
> >      ((1 5 1) (elemental-power (select-random '(earth water air
fire))))
> >      ((0 2 4) (magical-keen-edge))
> >      ((0 0 2) (append (random-sword-magic-power 'medium)
> >                       (random-sword-magic-power 'medium)))))
> > ...
> > I'm not trying to say that all applications are like my application,
> > and I'm not trying to say that my application can't be written in
> > Python.  I'm just saying that using macros, a paradigm that Python
> > doesn't even come close to supporting, makes reading and writing
> > functions like the above a lot easier.  You don't even need to know
> > that 'choose-random-assoc' is a macro, you just need to know how to
> > use it.  Heck, defun is a macro in Clisp.
>
> I don't understand why choose-random-assoc needs to be a macro instead
> of a function in the above example.

In order to avoid infinite recursion of random-sword-magic-power without
explicitly stating the base case (which is buried in the unpublished
macro).  See my last posts on this thread where I missed that also and then
added the line that makes the Python function version work.

>
> > I challenge anyone to come up with a better way to express the above
> > function in Python.  If I think it's better, I'll write "PYTHON RULZ"
> > on my forehead and post a photo on the web.
>
> This doesn't look too bad:
>
> def random_sword_magic_power (quality):
>   prob_list = [(5, 0, 0, power.glows_in_the_dark),
>                (3, 3, 0, power.magically_silent),
>                (1, 5, 1, power.elemental_power,
>                            random.choice(('earth','water','air','fire))),
>                (0, 2, 4, power.magical_keen_edge),
>                (0, 0, 2, random_sword_magic_power('medium') +
>                            random_sword_magic_power('medium'))]

You have no base case that I see to stop the recursion.

>   quality_level = ('poor', 'medium', 'good').index(quality)
>   return [weighted_random_choice(quality_level, prob_list)]

Terry J. Reedy





More information about the Python-list mailing list