nullprogram.com/blog/2009/07/03/
Earlier this year I implemented the
RinkWorks fantasy name generator in Perl. I think lisp lends
itself even better for that, and so I have a partial elisp
implementation for you.
What stands out for me is that the patterns can easily be represented
as a S-expression. We represent substitutions with symbols, literals
with strings, and groups with lists. For example, this pattern,
s(ith|<'C>)V
can be represented in code as,
I want a function I can apply to this to generate a name. First, I set
up an association list with symbols and its replacements,
Since we will need this in a couple places, make a function to
randomly select an element from a list,
A function for replacing a symbol,
And finally, the generator. Find a string, pass it through, find a
symbol, substitute it, find a list, pick one element and recurse on
it.
That's it! We can apply it to the expression above,
But that's really the easy part. The hard part would be converting the
original pattern into the S-expression, which I don't plan on doing
right now.
Something else to note: this is thousands of times faster than the
Perl version I wrote earlier.
I threw the code in with the rest of my name generation code
(namegen.el),
git clone git://github.com/skeeto/fantasyname.git
S-expressions are handy anywhere.