Michel Talon <talon at lpthe.jussieu.fr> writes:
> From the lisp side i have this code snippet which i have tested as working:
>
> (incf index)
> ;; Introduce variables z0, z1, etc. to represent f, f', etc.
> (setq newvar (intern (concatenate 'string "z" (write-to-string index))))
> (push newvar var-list)
>
> The relevant line is the third one, note one has to represent the prefix and
> the index as strings and then one can concatenate them. After that one creates
> a lisp symbol by "intern".
A couple of comments. Firstly, the concat-based solution from Pawe? is
probably more sensible. Or if you don't care what the numbers are, see
the gensym function.
Right, now I've been sensible, I'll say some stuff about the lisp
approach. Note that in Common Lisp,
(intern (concatenate 'string "z" (write-to-string 42)))
produces a symbol that you'd normally write as '|z42| rather than 'z42
(which is the same as 'Z42 and '|Z42|). For example:
(%i1) :lisp (eq (intern (concatenate 'string "z" (write-to-string 42))) 'z42)
NIL
If you don't want this, use capitals:
(%i1) :lisp (eq (intern (concatenate 'string "Z" (write-to-string 42))) 'z42)
T
Also, you haven't defined var-list here. Assuming that Jerome wants a
list of variable names (which won't have a hyphen in it because it's a
Maxima symbol), you could do something like this:
(%i1) var_list: [];
(%o1) []
(%i2) :lisp $var_list
((MLIST SIMP))
(%i2) :lisp (push (intern (format nil "$Z~A" 1)) (cdr $var_list))
($Z1)
(%i2) :lisp $var_list
((MLIST SIMP) $Z1)
(%i2) :lisp (push (intern (format nil "$Z~A" 2)) (cdr $var_list))
($Z2 $Z1)
(%i2) :lisp $var_list
((MLIST SIMP) $Z2 $Z1)
(%i2) var_list;
(%o2) [z2, z1]
And so on and so forth. I've used (format nil ...) rather than
(concatenate 'string ...) because it's a bit shorter and looks less
bizarre. The $ prefixes everywhere are what Maxima variables are called
in lisp.
But, yuck. Allowing yourself to write a lisp file, you could do
something a bit nicer, like this:
(defmfun $symbol_list (prefix min max)
(unless (stringp prefix) (merror "prefix must be a string."))
(unless (typep min '(integer 0)) (merror "min must be a non-negative integer."))
(unless (and (integerp max) (<= min max))
(merror "max must be an integer with min <= max."))
(cons
'(mlist)
(loop
for i from min to max
collecting (intern (format nil "~A~A" (string-upcase prefix) i)))))
Giving:
(%i5) load("tmp.lisp");
(%o5) tmp.lisp
(%i6) symbol_list("biggles", 1, 4);
(%o6) [biggles1, biggles2, biggles3, biggles4]
(Note: I haven't tested all the error handling etc: I wrote this pretty
quickly)
If you're wondering what the MLIST bit is, it's the top-level operator
for lists in Maxima (although op() returns "[", rather confusingly)
Right, time to get back to LaTeX-wrestling[*],
Rupert
[*] A sport banned in many countries but available on certain
pay-per-view channels, no doubt...
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 315 bytes
Desc: not available
URL: <http://www.math.utexas.edu/pipermail/maxima/attachments/20120628/88824574/attachment-0001.pgp>