General lisp: is there a less painful way to input math expressions?

I like the general lisp, but sometimes it is very painful to introduce simple math expressions like

a(8b^2+1)+4bc(4b^2+1)

(Of course, I can convert this, but it's slow, I write (+ () ()) first, and then put (* () ()) in each bracket ...)

I am wondering if anyone here knows the best way to introduce this. I was thinking of 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.

Does anyone have any better suggestions?

+5
source share
3 answers

There are reader macros for this purpose.

See: http://www.cliki.net/infix

For instance:

CL-USER 17 > '#I(a*(8*b^^2+1)+ 4*b*c*(4*b^^2+1) )
(+ (* A (+ (* 8 (EXPT B 2)) 1)) (* 4 B C (+ (* 4 (EXPT B 2)) 1)))

' - . #I( some-infix-expression ) - .

+25

, , - "" ". - " ", s-, , . ( , , , .)

, , Common Lisp; infix, :

{1 + {2 * 3} + {4 exp 5}}

(+ 1 (* 2 3) (exp 4 5))

.

+2

cl- , . ugly-tiny-infix-macro.

:

($ a * ($ 8 * (expt b 2) + 1) + 4 * b * c * ($ 4 * (expt b 2) + 1))

(+ (* A (+ (* 8 (EXPT B 2)) 1)) (* (* (* 4 B) C) (+ (* 4 (EXPT B 2)) 1)))

: $ - . , , / , .

, :

($ 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))

, , lisp.

($ 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))

It’s easier for me to reason and more profitable than the reader’s macro, because it can be easily mixed with lisp forms , so you can embed lisp expressions in the form. For example, it (exp b 2)can be any form of lisp, for example, (max a b c)or your own user (foobar a b c).

Further information on README can be found on github. It is also available in quicklisp.

0
source

All Articles