[OPINION] - does language really matter if they all do the samething?
Dietrich Epp
dietrich at zdome.net
Sun Jan 25 04:35:28 EST 2004
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.
So....
(choose-random-assoc color (red green) ((2 1) foo) ((1 1) bar))
...has a 2/3 chance of producing 'foo' when color=red, but only 1/2
when color=green.
My original point was that Python is not completely agnostic. There's
a method to programming in Python, and some patterns just don't fit
that method. I anticipated people asking for examples, and I expected
that a couple of my examples would fall flat. While Python is a
fantastic general-purpose language, it will often be beaten by
application-specific languages, but only in their domain. I
purposefully didn't use any examples of application-specific languages
because people would tell me that I were not being fair.
I still have no idea how to express the function in Python without
adding "crutches".
More information about the Python-list
mailing list