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

Aahz aahz at pythoncraft.com
Sun Jan 25 16:14:10 CET 2004


In article <mailman.758.1075023273.12720.python-list at python.org>,
Dietrich Epp  <dietrich at zdome.net> wrote:
>On Jan 24, 2004, at 3:24 PM, Paul Prescod wrote:
>>
>> function random_sword_magic_power(quality):
>>     return choose_random_assoc(
>>         selector = (quality, (poor, medium, good)),
>>         properties = {        (5, 0, 0): glows_in_the_dark(),
>>                               (3, 3, 0): magically_silent(),
>>                         (1, 5, 1): elemental_power(
>>                           choice([earth, water, air, fire])
>>                         (0, 2, 4): magical_keen_edge(),
>>                         (0, 0, 2): ????}
>>
>> I don't follow what you are doing with the last line so I didn't 
>> duplicate it.
>
>The last line recursively calls random_sword_magic_power() twice and 
>concatenates the results.  It's something that I used a lot in this 
>program, which I originally wrote in Python, but I was completely 
>baffled trying to implement this part.  The reason you can't do it with 
>a dict or list in Python is that the list is always evaluated, and 
>recurses infinitely.  A dict isn't appropriate because there would be 
>duplicate keys.  Think of it as three tables collapsed into one, the 
>'poor', 'medium', and 'good' tables, where the relative probabilities 
>for each table fall on a different column.

Well, choose_random_assoc() needs to dispatch on functions rather than
values.  That will recurse only one level, and only in
choose_random_assoc().  You'll also need to identify tuples in order to
pass in values.  Here's a rewrite of Paul's function that shows roughly
what you need to do:

function random_sword_magic_power(quality):
    return choose_random_assoc(
        selector = (quality, (poor, medium, good)),
        properties = {
            (5, 0, 0): glows_in_the_dark,
            (3, 3, 0): magically_silent,
            (1, 5, 1): (elemental_power, choice([earth, water, air, fire]),
            (0, 2, 4): magical_keen_edge,
            (0, 0, 2): ((random_sword_magic_power, medium),
                (random_sword_magic_power, medium)),
            }

Quite frankly, though, for this kind of application, I probably would
choose to write a domain-specific language (more precisely, a "smart"
config file format).  That would make it easier for someone who wasn't a
programmer to add functionality.  As I said earlier, I don't think the
fact that Lisp makes it easy to directly express this idiom makes up for
its other faults.
-- 
Aahz (aahz at pythoncraft.com)           <*>         http://www.pythoncraft.com/

"The joy of coding Python should be in seeing short, concise, readable
classes that express a lot of action in a small amount of clear code -- 
not in reams of trivial code that bores the reader to death."  --GvR



More information about the Python-list mailing list