In my experience at least, there is a tradition of people looking over other
peoples' lisp code and trying to "improve" it,
making it shorter and/or faster or more idiomatic. (It is in the grand
tradition of "hacking", in a positive sense).
I just looked at hypgeo, and see a bunch of stuff that could be
hacked/improved..
There is a case construction in CL.
(cond ((eq flag 'a) ...)
((eq flag 'b) ...)
....
can be written as
(case flag (a ...)
(b ...)
...
Also
(cond (r(return a))
(cond (s(return b))
can be
(return (cond (r a)
(s b)))
Also,
any lisp program that is longer than about 40 lines of code is suspicious.
like lt-sf-log, which has many pieces all of, or most of which, look like
(cond ((setq l (testn u))(setq ....)(return(fractest ....)))
...
one way of rethinking/rewriting such a program is to make it data directed,
or object oriented.
for example I'm looking at line 1194 of hypgeo, rewrite as...
(defvar *lt-sf-tests*
'((#'twoj . #'(lambda(L)(lt2j (cdras 'u L)(cdras 'w2)...)
(#'twoh . #'(lambda(L) (fractest ...) ....)) ...
and then
(defun lt-sf-log(u)
(dolist (k *lt-sf-tests*)
(setf L (funcall (car k) u))
(if L (return (funcall (cdr k) L)))
(return 'other-j-cases-next))
;; that's all
now it turns out that cdras is defined elsewhere, and it is actually kind of
wasteful.
it is
(defun cdras (a b)
(cdr (assoc a b :test #'equal)))
but if argument a is a literal symbol like 'w2, then the simpler and faster
test would be #'eq
or if you just leave out the test.
thus you would be better off doing
(defun cdras(a b)
(cdr (assoc a b)))
or maybe more idiomatically
(defun cdras (a b)
(cdr (find a b :key #'car)))
and if you wanted to almost always have as the second argument, the symbol
L,
you could do this
(defun cdras(a &optional (b L))
(cdr (find a b :key #'car)))
and then you could just say (cdras 'w1). [unless you look at the hypgeo
source code, this may not make sense...]
and if you got tired of seeing the 'w1 and just wanted w1, you could do this
(defmacro cdras(a &optional(b 'L))
`(cdr (find ',a ,b :key #'car)))
then if you said (cdras w2) it would come out as
(macroexpand '(cdras w2 )) -->
(cdr (find 'w2 L :key #'car))
and this would probably be expanded inline without a function call, at least
for some lisps.
next you might want to change the name to something more meaningful. You
are extracting the binding from the association list that is the result of
matching using Moses' m2 program, so maybe
I think (m2bind w2) would be nicer.
thus
(defmacro m2bind (a &optional(b 'L)) ;get binding from assoc list from m2
match for name a, e.g. (m2bind v)
`(cdr (find ',a ,b :key #'car)))
So in consequence, code like
(cond ((setq l (twoy u))
1220 (setq index1 (cdras 'v1 l)
1221 index2 (cdras 'v2 l)
1222 arg1 (cdras 'w1 l)
1223 arg2 (cdras 'w2 l)
1224 rest (cdras 'u l))
1225 (return (fractest rest
1226 arg1
1227 arg2
1228 index1
1229 nil
1230 index2
1231 nil
1232 '2ytj))))
would become
data like ...
.... (#'twoy . #'(lambda(L)(fractest (m2bind u)(m2bind w1)(m2bind
v1)nil(m2bind v2)nil '2ytj))
Of course, if a program works, it is always hazardous to mess with it.
It would be nice if people who worked on the code also put their names in
the header!
I'm really really happy that people are getting this code to work better.
RJF
> -----Original Message-----
> From: maxima-bounces at math.utexas.edu
> [mailto:maxima-bounces at math.utexas.edu] On Behalf Of Dieter Kaiser
> Sent: Thursday, June 26, 2008 3:12 PM
> To: 'Robert Dodier'
> Cc: maxima at math.utexas.edu
> Subject: Re: [Maxima] Further work on $specint
>
> I will do my best and try to adapt the "good" Lisp style.
> Please give me a hint
> if you have problems with my style of programing.
>
> Im learning Lisp now since 1 1/2 years. Lisp in combination
> with Maxima is
> wonderful to program mathematic. At first Lisp seems to me
> very strange, but
> after learning the principles and the different way of
> programing in Lisp, it's
> much more productiv as C++ for me. A lot of things one need to program
> mathematic are just part of Lisp or Maxima. I like it very much.
>
> Dieter Kaiser
>
> -----Urspr?ngliche Nachricht-----
> Von: maxima-bounces at math.utexas.edu
> [mailto:maxima-bounces at math.utexas.edu] Im
> Auftrag von Robert Dodier
> Gesendet: Donnerstag, 26. Juni 2008 17:26
> An: Raymond Toy (RT/EUS)
> Cc: maxima at math.utexas.edu
> Betreff: Re: [Maxima] Further work on $specint
>
> On Thu, Jun 26, 2008 at 7:32 AM, Raymond Toy (RT/EUS)
> <raymond.toy at ericsson.com> wrote:
>
> > The only request I have is please don't put single parens
> on lines by
> > themselves as if they were braces for C. That's considered
> pretty bad
> > Lisp style. All parens should be bunched together on the same line.
>
> Agreed 100%.
>
> best
>
> Robert Dodier
> _______________________________________________
> Maxima mailing list
> Maxima at math.utexas.edu
> http://www.math.utexas.edu/mailman/listinfo/maxima
>
> _______________________________________________
> Maxima mailing list
> Maxima at math.utexas.edu
> http://www.math.utexas.edu/mailman/listinfo/maxima
>