Tuesday, August 10, 2010

Lisp by Analogy (using Kanji and Hebrew)

I really like programming with the Lisp programming language and, even though I rarely do much Lisp programming these days, it has affected how I program in other languages. In the past, I've blogged a lot about both Lisp macros and continuations; however, I still occasionally get questions from non-lispers regarding Lisp macros and continuations as most programming languages don't support them to the same extent that Lisp does. It's understandable that many people struggle with grok'ing the concepts as it's kinda like the Bedouin trying to understand icebergs ;-). With some people, it helps to use analogies, so I'll try to do that here. As with most analogies, mine will be a bit flawed and I'll intentionally be glossing over and simplifying things; however, I hope these analogies will give non-lispers a bit of a "feel" for what Lisp macros and continuations are like.

Macros

A major differentiator of Lisp is it's ability to create custom, domain-specific languages. A key "enabler" for this is Lisp macros which give the programmer the ability to create new language constructs (macros in many programming languages only provide a text substitution mechanism). I'll use a natural language analogy to try to illustrate how this is different from what non-Lisp languages provide. Both Indo-European and Semitic written languages create words by combining letters of the alphabet that represent the individual sounds that make up a word. So, in English, the word for a female spouse is "wife" while, in French, it is "├ępouse". In English, the written word "wife" is the combination of symbols that represent the sounds associated with the letters "w", "i", "f", and "e", so it has a different written representation from the same concept in French. In some countries in the East (most commonly, in China and Japan), Kanji characters can be used for written words. Kanji is different from most other written script in that there is no attempt to represent the spoken word in the written representation of the word. Instead, it uses a pictogram-like representation of the word. In both China and Japan, although the spoken languages are very different, the Kanji representation of many words is the same (differences arise because new words were subsequently added in one country but not in the other). So, for example, in Kanji, the word for a female spouse is actually a pictogram representing the combination of the Kanji pictograms for "woman" + "holding" + "broom-like object" as depicted below:

The word 'wife' in Kanji

Therefore, once a new pictogram has been composed and accepted for use, that pictogram is the same as any other pictogram in Kanji. While in English, there are 26 symbols (letters of the alphabet) that represent the basic "building blocks" of the written language and you can't add any more, in Kanji, there are thousands of symbols (pictograms) that represent the basic "building blocks". In the same way, when a Lisper creates a macro, he is creating a new "building block" in Lisp. The new macro will be the same as any other "built-in" language construct (while there are some things that you can do with a function, there are some things that can only be done with a macro). So, if (for example) a Lisper wants to add a new looping construct to Lisp, he might create an "iterate" macro in his custom language. This new construct (when used in the custom DSL) would be no different from "standard" built-ins like "do" or "if". So, in this way, macros are analogous to Kanji - you are not limited to a core set of "building block" constructs in the language and you can expand that set of core constructs as/when needed.

Continuations

Continuations give programmers the ability to save the execution state at any point and return to that point at a later point in the program. In the past, I've talked a lot about using continuations to develop complex web applications. I particularly liked Avi Bryant's "elevator pitch" definition of why continuations are useful for web apps:

"Continuations bring precisely the same benefits to traditional web development that subroutines bring to GOTOs"

However, continuations can be used for other purposes as well. I'll try to use a personal analogy to illustrate what using continuations is like. Music often has the ability to take you to a particular place or state of mind. In October 1973 I had just started university in the US when the Yom Kippur War broke out. I dropped out of university and flew back to Israel. There was a song that was playing on the radio quite frequently at that time in Israel. It was called "Lu Yehi" (roughly translated as "Let it be" or "May it be") and it really captured the feelings and the mood of the country at that time. A translation of the lyrics is available online; however, as with most songs (and most translations), the original is best and translations don't really do it justice. Anyhow, flash forward to the year 2008: I hadn't been back to Israel since leaving in 1980 and I hadn't spoken Hebrew in almost as long. Then, one evening, I was having a drink and looking out at the lights of the ships in the bay when I started humming some of the words to the song. Not expecting to find anything, I did a search on YouTube. To my surprise, it was there! I pressed "play" and suddenly, I was back in Israel 35 years ago. As I listened to the Hebrew words, all the same feelings, thoughts, and emotions from that time flooded back.

That's what continuations provide to a programming language - the ability to jump back to a particular place in the code, retaining all of the program "state" that was in place at that time.

So, I don't know whether these analogies worked for you or not, but, for me, they capture part of the essence of these two key features of Lisp.

0 comments: