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

Paul Rubin http
Mon Feb 16 03:32:36 EST 2004


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.

> 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'))]
  quality_level = ('poor', 'medium', 'good').index(quality)
  return [weighted_random_choice(quality_level, prob_list)]



More information about the Python-list mailing list