Monday, March 16, 2009

Ghetto Langanges...

There are two different versions of English, the one we claim in our grammar books and the one we use from day to day. Literature seems to be written with textbooks while popular culture bastardizes the language at every chance. This is what I refer to as a "Ghetto Language". As Bruce Willis said, "I only speak two languages: English and bad English!"

The textbook is useful for training people how to think about sentence structure and to serve as a guide when writing. It formalizes what is allowed and keeps the language from splintering into a million different ones. This is important since we might need to communicate with people from other regions and backgrounds. Proper language then serves as a sort of Least Common Denominator for its users.

Ghetto Languages are different. They are used for various purposes, such as expressing feelings or obfuscating its meaning from other groups. Some are there as a barrier; others as a tool. Sometimes it hardly resembles English at all. Still, for each use, the new sub-language works better than plain English. New words and concepts fuse into new synergistic forms. Poets evolve and renew our language. Old thoughts fall out as new ones replace them.

C++ is a proper language. It has very particular rules, some of which may not make sense but it is mostly consistent. Everything has its place and classes and functions define its structure. There are rules to programming it properly such as using human readable names and making function names verbs.

Perl on the other hand is what happens to C after dark. The rules are kept as suggestions, but soon, new constructs appear. Words like die, grep, and split join if, else, and while. Variables are hidden, assumed by context like pronouns. Data types are blurred and powerful mini-languages are included. It is interpreted instead of compiled, and a Read-Eval-Print-Loop is born. Fast development and data manipulation is replaces small code size and speed.

The differences are between what we want, and what we need. In C, I now have a library of tools that I just include upfront, such as regular expressions, cons_cells, sort routines, and more. I rely on the compiler to strip out what I don't need. Perl includes almost everything I need all the time. I started out writing my Perl like C. After a while I started writing my C like Perl.

What happens then, when you can change the language syntax on the fly? This weekend I needed Perl style regular expressions, but I never liked the default CL-PPCRE command syntax. I just wanted to type out my regular expression, give it a couple of modifiers and go! So I modified some reader macros I found on and created this:

(#s/Sucks|Su\w+/is great/i "Bender sucks!")
>"Bender is great!"
(#m/(Fo+)/gi "Foo fooo foo fo foo foo Bar!!!")
>("Foo" "fooo" "foo" "fo" "foo" "foo")
The only problem was I didn't realize that #s and #r are already used! Still, it was neat to do and soon I will change the syntax to #@ unless I get a better idea. And after that I want to create Perl style strings. That should make my web pages easier to write!

You might ask, what's the point? If I wanted Perl I can use Perl. Yes, but I do a lot of data manipulation and transformation. I like Perl's regular expressions but I don't like tying to guess what a hash to a hash to a list command looks like. For that, Lisp is perfect. Any yes, I could have used the cl-ppcre library without the syntactic sugar, but I would have to think more about the implementation than about the problem. By creating a simple reader-macro that makes a regular expression lambda function I can also create perl-style one-liners like this:
(File-By-Line "" #s/Che+[sz][ie]+st/cheese/gi :inplace bk)
I mean that's pretty close in length to:
perl -pei.bk "s/Che+[sz][ie]+st/cheese/gi"
and it makes more sense. This way I don't have to look into the cl-ppcre package and remember how it works. I mean
(lambda (x) (regex-replace-all (regex-create-scanner "Che+[sz][ie]+st" :case-insensitive-mode :global-mode) "cheese" x))
is a little unwieldy.

Finally, this demonstrates how the Lisp language can be changed on the fly to fit your need. Most languages force you to change the problem to the language. And after creating this simple change, I wonder how many other useful ghetto additions have been developed for Lisp? Maybe we can put them together into a CL-GHETTO package for the Lisp-Speaking Community?

No comments:

Post a Comment