From: Nonzero
Subject: Converting LISP math statements to 'Standard' math statements.
Date: 
Message-ID: <2e3cbcd8.0110300844.8a4f6d0@posting.google.com>
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

From: Tim Bradshaw
Subject: Re: Converting LISP math statements to 'Standard' math statements.
Date: 
Message-ID: <nkjofmpgguq.fsf@omega.tardis.ed.ac.uk>
··········@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))
From: ········@acm.org
Subject: Re: Converting LISP math statements to 'Standard' math statements.
Date: 
Message-ID: <5ABD7.10861$6h5.1181458@news20.bellglobal.com>
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? 
From: Tim Bradshaw
Subject: Re: Converting LISP math statements to 'Standard' math statements.
Date: 
Message-ID: <nkjk7xct8x0.fsf@omega.tardis.ed.ac.uk>
········@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