Here’s an implementation of infix notation for Shen; it’s effectively Dijkstra’s shunting-yard algorithm. Custom precedence can be defined by setting prec. (define prec ** -> 4 * -> 3 / -> 3 + -> 2 - -> 2) * power is defined in the maths library *\ (define ** X Y -> (power X Y)) (define shunt  Output  -> Output   [X Op Y | Rest] -> (shunt [Op] [(shunt   Y) (shunt   X)] Rest) where (element?
More Shen macros today. In the previous post I promised an explanation for why we don’t have/need quasiquote, unquote and unquote-splicing in Shen. Let’s look at a Common Lisp example: a single place let. This would typically be written as: (defmacro let-one ((loc val) &rest body) `(let ((,loc ,val)) ,@body)) …using quasiquote, unquote and unquote-splicing. This is actually short hand for the following: (defmacro let-one ((loc val) &rest body) (append (list 'let (list (list loc val))) body)) I think everyone would agree that this second form is harder to read.
In this post I’m going to concentrate on Shen macros; some familiarity with basic Shen and Common Lisp macros is assumed. In Shen, as in traditional Lisp style, code is data and data is code. Shen code is read in as a list datastructure: (+ 1 2) becomes [+ 1 2]. Shen macros are functions that pattern match on the list representation of code at read time in order to rewrite it.