I enjoy common lisp, but sometimes it is really painful to input simple math expressions like
a(8b^2+1)+4bc(4b^2+1)
(Sure I can convert this, but it is kind of slow, I write (+ () ()) first, and then in each bracket I put (* () ())...)
I'm wondering if anyone here knows a better way to input this. I was thinking about writing a math macro, where
(math “a(8b^2+1)+4bc(4b^2+1)”)
expands to
(+ (* a (1+ (* 8 b b))) (* 4 b c (1+ (* 4 b b))))
but parsing is a problem for variables whose names are long.
Anybody has better suggestions?
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:
It is expanded to
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:
Anything within parentheses at position of an operand is treated like a lisp form.
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.
There are reader macros for this purpose.
See: http://www.cliki.net/infix
For example:
'
is the usual quote.#I( some-infix-expression )
is the reader macro.One project I'm keeping an eye on are so-called "Sweet Expressions". The goal of the project is to add "sweetening" that is backwards-compatible with s-expressions, and simple enough that the expressions won't get in the way of macros. (It has been observed, for example, that operator precedence really interferes with the macro system; hence, the proposed solution doesn't use operator precedence.)
It should be kept in mind that the project is in its infancy, particularly for Common Lisp; however, the project has a working implementation of infix notation, which relies on curly brackets and a simple algorithm:
translates nicely into
I'll just refer you to the link for a more in-depth discussion of the semantics of curly-braces.