|Pathologically Eclectic Rubbish Lister|
Perl can do it, take 1 (sentence generation)by spurperl (Priest)
|on Jun 17, 2005 at 14:54 UTC||Need Help??|
As I mentioned here, I want to practice taking good, elegant Lisp code and re-writing it in Perl. And the first opportunity for this came today.
While browsing Peter Norvig's website, I came upon this - a primer in Python for Lisp programmers. In the end, in a section called "Comparing Lisp and Python Programs", Norvig takes an excerpt from his Paradigms of Artificial Programming (one of the best programming book in the world, IMHO) - generation of random sentences from defined grammars, and implements a Python version for it.
So, I decided to employ Perl to compete in this task as well...
Here is an implementation I came up with (only generate, as generate_tree is the same idea):
Overall I'm satisfied with the result - it's an elegant functional code that represents the solution in an intuitive recursive way, without too much overhead.
Generally, this code is "the Lisp way", or rather "the functional programming way". There may be more "perly" solution that are more succinct (I don't mean golf !), efficient and so on. You are welcome to propose other methods and / or observations.
P.S: implementations in Perl 6 will be also most welcome !