I recently wrote a cl macro exactly for this purpose, you might find it useful. It's called ugly-tiny-infix-macro.
You could write the expression in question as:
($ a * ($ 8 * (expt b 2) + 1) + 4 * b * c * ($ 4 * (expt b 2) + 1))
It is expanded to
(+ (* A (+ (* 8 (EXPT B 2)) 1)) (* (* (* 4 B) C) (+ (* 4 (EXPT B 2)) 1)))
Explanation:
$ is the name of macro. The arguments are considered as a list of expressions and hence the liberal use of whitespace to separate numbers/forms from symbols that denote operators.
Consider the following examples to understand function of this macro better:
($ 1 + 2) ; gets converted to (+ 1 2), where name of the macro is $
($ t and nil) ; gets converted to (and t nil)
($ 3 > 5) ; gets converted to (> 3 5)
($ 1 + 2 + 3) ; gets converted to (+ (+ 1 2) 3)
($ 1 + 2 * 3) ; gets converted to (+ 1 (* 2 3))
($ 1 < 2 and 2 < 3) ; gets converted to (AND (< 1 2) (< 2 3))
Anything within parentheses at position of an operand is treated like a lisp form.
($ 2 + (max 9 10 11)) ; gets converted to (+ 2 (max 9 10 11)). It could have been any function / lisp form.
($ 6 / ($ 1 + 2)) ; gets converted to (/ 6 ($ 1 + 2)), and then subsequently to (/6 (+ 1 2))
I find it easier to reason about and more advantageous than a reader macro, since it may be easily intermixed with lisp forms, so you can nest lisp forms within the expression. For example, the (exp b 2)
could have been any lisp form, like (max a b c)
or your own user defined (foobar a b c)
.
You can find more information on the README on github. It's also available on quicklisp.
ab
asa*b
andab
as an identifier. – Jermainejerman