M-Expressions and the Geek Transformation: A Nostalgic Tale
Nostalgic M-Expression
I recently revisited the ancient book “Anatomy of LISP” by JHON ALLEN. Even today, it remains a captivating read, covering both theory and implementation. The Lisp code within it is written in M-expression.
M-expression was used in the early days of Lisp, but subsequently, S-expression became the mainstream, and nowadays, writing Lisp in M-expression is virtually unheard of. However, I believe that M-expression still has its significance in the modern world. I’d like to take a moment to elaborate on that.
Appearances Matter
Back in my student days, when I played the guitar in a rock band, I was rather popular with the ladies. However, my fascination with computers transformed me into a computer geek. My hair became unkempt, my attire became careless, and I spent my days scouring computer shops for interesting components. I found that I no longer attracted the attention of women, unless, of course, I happened to be a billionaire like Bill Gates. A poor first impression can turn women away.
10年ぶりにエレキを弾いたよ。 Kill the king Rainbow — YouTube
(Author, returning to his guitar-playing roots after 40 years)
It might be a bit of a stretch, but doesn’t S-expression Lisp resemble a similar situation? With a more conventional syntax like Ruby, you can make a better first impression and avoid suspicion.
Embracing M-Expression with Ruby Flavor
So, I’ve prepared a playful library called ‘mexp’ for my custom Lisp interpreter/compiler, Easy-ISLisp. It converts that old-fashioned M-expression into S-expression and executes it. Take a look at the examples below.
Easy-ISLisp Ver3.50
> (import "mexp")
> T
> (mexp)
Meta expression translator
M> load["example/foo.m"]
T
M> fact[10]
3628800
M> fib[20]
6765
M>
example/foo.m
;; M-expression samples
foo[x] <= x+1
fact[n] <= [n=0->1;
t->n*fact[n-1]]
fib[n] <= [n=0->0;
n=1->1;
t->fib[n-1]+fib[n-2]]
For those familiar with old Lisp, this is the nostalgic M-expression. But for the younger generation who might not know it, it might resemble Ruby. If it does, mission accomplished. I look like an ordinary guy. I’ll keep my geeky nature hidden.
Befriending S-Expressions
Now that I’ve passed the first impression test, I’ll gradually reveal its true power — the geek mode. Use ‘sexp[fn]’ to convert to S-expression and write to a file.
M> sexp["example/foo.m"]
T
M>
This generates ‘foo.lsp’ with S-expressions converted from M-expressions. With some formatting, you get properly indented S-expressions.
;; foo.lsp
(DEFUN FOO (X) (+ X 1))
(DEFUN FACT (N) (COND ((= N 0) 1) (T (* N (FACT (- N 1))))))
(DEFUN FIB (N) (COND ((= N 0) 0) ((= N 1) 1) (T (+ (FIB (- N 1)) (FIB (- N 2))))))
> (import "formatter")
T
> (formatter "example/foo.lsp")
T
;;foo.lsp
(DEFUN FOO (X) (+ X 1))
(DEFUN FACT (N) (COND ((= N 0) 1) (T (* N (FACT (- N 1))))))
(DEFUN FIB
(N)
(COND ((= N 0) 0)
((= N 1) 1)
(T (+ (FIB (- N 1)) (FIB (- N 2))))))
The capitalization is just for fun. If you found it interesting to see how M-expression transforms into S-expression, then my mission is accomplished. Next, I might dive into macros or showcase some geeky powers. Perhaps, with this, we can witness the birth of a Lisp enthusiast. Who knows?
Easy-ISLisp
Easy-ISLisp is open-source software under the BSD license. Feel free to use it. sasagawa888/eisl: ISLisp interpreter/compiler (github.com)