I wonder if there are implementations of methods to write readable arithmetics in lisp/arc. I'm learning cl by trying to implement something small and interesting and wrote a code to get Google Maps tile for a given (lat lon zoom). To convert latitude into mercator latitude I had to write the following: (- (log (tan (+ (/ pi 4) (/ (radians lat) 2))))))) in php it would look like which is -log(tan(pi/4 + radians(lat)/2)) in maths. Why not make a function to parse such expressions and evaluate them? The second expression can be valid lisp code, and the code may look like (arith '(- log (tan (pi / 4 + radians (lat) / 2))) I've written a code to parse simple sequenses, like 2 ^ 2 + 2 * 3 ^ 4 (still without parenthesis), but maybe it is already done? (defun get-op (elem ops)
(cond
((atom ops) (if (equal elem ops) elem nil))
((equal elem (car ops)) (car ops))
(t (get-op elem (cdr ops)))))
(defun do-ops (expr ops)
(print expr)
(cond
((atom expr) expr)
((= (length expr) 1) (car expr))
((get-op (cadr expr) ops)
(do-ops
(cons
(funcall
(cadr expr)
(do-ops (car expr) ops)
(do-ops (cddr expr) ops))
(cdddr expr))
ops))
(t (cons
(car expr)
(list (cadr expr)
(do-ops (cddr expr) ops))))))
(do-ops '(5) '(+))
(do-ops '(5 + 2) '(+))
(do-ops (do-ops '(5 + 2 * 3) '(*)) '(+))
|