Bill Clementson's Blog

Bits and pieces (mostly Lisp-related) that I collect from the ether.

June 2008
Sun Mon Tue Wed Thu Fri Sat
1 2 3 4 5 6 7
8 9 10 11 12 13 14
15 16 17 18 19 20 21
22 23 24 25 26 27 28
29 30
May  Jul

Lisp by Analogy: Macros=Kanji, Continuations=YouTube

Monday, June 16, 2008

I've written a lot in the past about both Lisp macros (see here) and continuations (see here); however, I still get questions from non-lispers about macros and continuations. I guess it's kinda like trying to explain icebergs to the Bedouin ;-). 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: I've talked a lot about the ability of Lisp to create custom, domain-specific languages. A key "enabler" for this is Lisp macros. 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: I've also talked a lot about using continuations to develop complex web applications. I particularly liked Avi Bryant's "elevator pitch" definition of why contuations 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, I haven't been back to Israel since 1980 and I haven't spoken Hebrew in almost as long. The other evening, I was having a drink and looking out at the lights of the ships in the bay when I remembered 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. Let me know if you can think of better ones and I'll publish them as updates to this post.

emacs Copyright © 2008 by Bill Clementson