Hi Maxima hackers,
I'm having a hard time figuring out how to do some basic things in
maxima; it's probably a host of misconceptions colluding all at once.
How do you, for instance, recurse down a list in maxima? As a
textbook example, suppose I wanted to write a function filter which
takes a function f and a list and returns a list of only those
elements whose images under f are true. In Haskell, for instance, it
would just be:
filter f [] = []
filter f (x:xs) | f x = x : r
| otherwise = r
where r = filter f xs
or in scheme you could do
(define (filter f list)
(cond ((null? list) ())
((f (car list)) (cons (car list) (filter f (cdr list))))
(else (filter f (cdr list)))))
Now, for a moment setting aside a nice recursive definition, just take
the definition:
filter(cond,list):=
block([result],
result:[],
for l in list do (if p(l) then result:cons(l,result)),
return(result))$
which I think should at least work. (Hopefully I haven't already
strayed!) I realize it reverses the order of the list---I'm just
trying to get something that works at this point. Now, I add a new
layer of trouble: I try to define a conditional:
f(x):=x>0
then
f(2) => 2>0
but
f(2), pred => true
And if I compute
filter( f, [-2,-1,0,1,2] )
I'd hope to get [1,2] (or the reverse, whatever) but instead I get
MACSYMA was unable to evaluate the predicate:
** error while printing ERROR message **
MACSYMA was unable to evaluate the predicate:~%~M
#0: filter(cond=f,list=[-2,-1,0,1,2])(filter.mac line 3)
-- an error. Quitting. To debug this try DEBUGMODE(TRUE);)
If, on the other hand, I try:
g(x):=if x>0 then true else false;
it behaves differently than f:
g(2) => true
no ",pred" is needed, but I still get the same errors for
filter( g, [-2,-1,0,1,2] );
Is there a simple obvious way to go about all these things and a
simple way to understand what all this pred business is about? I
would guess the main problem is that I don't understand the evaluation
model and need to rtfm. Is there a good reference, especially for
programming in maxima?
Hopefully I haven't made myself an idiot in front of all the maxima
gurus! Ultimately, what I'd like to get working as a first use of
maxima is a little routine that computes the eigenvector corresponding
to the maximal real eigenvalue of a matrix. Phew, it seems pretty
distant at this point!
Also, I tried browsing some of the maxima source code for some clues.
Does one generally do programming directly within lisp rather than in
maxima?
Thanks,
Carl