Gah... What am I doing wrong here?



After repeated failed attempts to group common unit terms over the
addition operator in lisp, I finally decided to see if I could make
Maxima do it.  My first cut at it is below.  The idea is to take
advantage of the unit packages mtimes formatting, and it works OK up
until the end when the display gets hashed.  Can somebody tell me what
I'm doing wrong?

(%i27) a1;
                                                     2  3           2 
3
            kg           kg m         kg m         kg  m          kg  m
(%o27) (c) (--) + (- e) (----) + (a) (----) + (d) (------) + (b)
(------)
            s              2            2             4              4
                          s            s             s              s
(%i28) groupbyadd(a1);
[d + b, a - e, c] 
   2  3
 kg  m   kg m  kg
[------, ----, --] 
    4      2   s
   s      s
                       2  2                       4  6
     kg              kg  m                      kg  m
(c) (--) + ((- a e) (------)) ("*"()) + ((b d) (------)) ("*"()) 
     s                  4                          8
                       s                          s

groupbyadd(expression):= block([partswitch:true],
   i:1, j:1,
   unitslist : [],
   coeflist : [],
   nonunitelem : [],
   result1 : 0,
   while not(?equal(part(expression,i,1),end)) do (
      if ?equal(part(expression,i,2),end) then (
        notunitelem : cons(part(expression,i,1),nonunitelem)
      ) else (
      if lfreeof(unitslist,part(expression,i,2)) then (
         unitslist : cons(part(expression,i,2),unitslist),
	 coeflist : cons(part(expression,i,1),coeflist))
      else (
      for j : 1 thru length(unitslist) do (
        if ?equal(unitslist[j],part(expression,i,2)) then (
	  coeflist[j] : coeflist[j] + part(expression,i,1))))),
      i : i + 1),
    coeflist : map('?meval,coeflist),
    unitslist : map('?meval,unitslist),
    for i : 1 thru length(unitslist) do (
      result1 : result1 +
(ratsimp(coeflist[i])*ratsimp(unitslist[i]))),
    for i : 1 thru length(nonunitelem) do (
      result1 : result1 + nonunitelem[i]),
      print(coeflist),
      print(unitslist),
      print(result1),
    result1);


		
__________________________________ 
Do you Yahoo!? 
Make Yahoo! your home page 
http://www.yahoo.com/r/hs