Hello all
Does anyone know of a simple way on a Red Hat Linux system to
convert a math formula in LISP to a more standard for? (something
that could be understood by an excel like program?)
IE I want to go from something like:
(+ x (* x (/ x 7)))
To somethink like this:
(x+(x*(x/7))
Needless to say, the statements I want to convert are far too long to
do by hand.
Thanks in advance.
-Nonzero314
··········@hotmail.com (Nonzero) writes:
> Does anyone know of a simple way on a Red Hat Linux system to
> convert a math formula in LISP to a more standard for? (something
> that could be understood by an excel like program?)
>
I hope this is not homework... The following does at least some of
this. It is *not* particularly good code - there are obvious
abstractions missing and it generally over-parenthesizes, but it did
what I needed.
--tim
--cut--
;;;; Infixify hack.
;;; This is not meant to be clever. I think it is sometimes right
;;; though.
(in-package :cl-user)
(defgeneric infixify-expression (form stream))
(defmethod infixify-expression ((form cons) stream)
(infixify-operator (first form) (rest form) stream))
(defmethod infixify-expression ((form t) stream)
(prin1 form stream))
(defgeneric infixify-operator (op args stream))
(defmethod infixify-operator ((op t) args stream)
(format stream "~S(" op)
(loop for a on args
for (e . rest) = a
do (infixify-expression e stream)
when rest do (princ ", " stream)
finally do (princ ")" stream)))
(defmethod infixify-operator ((op (eql '+)) args stream)
(cond ((null (rest args))
(infixify-expression (first args) stream))
(t
(princ "(" stream)
(infixify-expression (first args) stream)
(princ " + " stream)
(infixify-operator op (rest args) stream)
(princ ")" stream))))
(defmethod infixify-operator ((op (eql '*)) args stream)
(cond ((null (rest args))
(infixify-expression (first args) stream))
(t
(princ "(" stream)
(infixify-expression (first args) stream)
(princ " * " stream)
(infixify-operator op (rest args) stream)
(princ ")" stream))))
(defmethod infixify-operator ((op (eql '-)) args stream)
(cond ((null (rest args))
(princ "(-" stream)
(infixify-expression (first args) stream)
(princ ")" stream))
(t
(princ "(" stream)
(infixify-expression (first args) stream)
(princ " - " stream)
(infixify-operator '+ (rest args) stream)
(princ ")" stream))))
(defmethod infixify-operator ((op (eql '/)) args stream)
(princ "(" stream)
(infixify-expression (first args) stream)
(princ " / " stream)
(infixify-operator '* (rest args) stream)
(princ ")" stream))
Tim Bradshaw <···@tfeb.org> writes:
> I hope this is not homework... The following does at least some of
> this. It is *not* particularly good code - there are obvious
> abstractions missing and it generally over-parenthesizes, but it did
> what I needed.
If it _was_ homework, then using DEFMETHOD would probably be
contra-indicated, because [typically] Lisp is just used to process
lists using CAR/CDR and recursion. After all, that's in the
encyclopaedia, isn't it? :-)
--
(reverse (concatenate 'string ·············@" "sirhc"))
http://www.cbbrowne.com/info/unix.html
What would the world be like with no hypothetical situations?
········@acm.org writes:
> If it _was_ homework, then using DEFMETHOD would probably be
> contra-indicated, because [typically] Lisp is just used to process
> lists using CAR/CDR and recursion. After all, that's in the
> encyclopaedia, isn't it? :-)
Let alone using methods defined on built-in classes, everyone knows you can't do that in C++OO languages.
--tim