Using lisp code in maxima - lisp

I want to use (make-array '(4 3 8)) in maxima which is basically to generate multi-d matrix as I am not able to find API to create multi-d matrices including with array(name,d1,d2...dm).
I can execute it using :lisp (make-array '(4 3 8)) but I don't know how I can label it as something like,
arr: :lisp(make-array '(4 3 8))
I also want to know if it is possible to use lisp code inside maxima functions. Any sort of help shall be highly regarded.

To create a named array in Lisp code just exactly the same as array(name, d1, d2, ..., dm), write:
(mfuncall '$array name d1 d2 ... dm)
You can't include Lisp code directly in Maxima functions. But you can call Lisp functions. If the lisp function is named $foo, then in Maxima it's foo; if in Lisp it's foo, then in Maxima it's ?foo. E.g.:
:lisp (defun $foo (x) ...)
f(x) := print (foo (x));
By the way, Maxima's treatment of arrays is still a mess ... maybe someday we'll clean it up.

You can use make_array to create arrays directly:
(%i18) make_array(fixnum,4,3,8);
(%o18) {Array: #3A(((0 0 0 0 0 0 0 0) (0 0 0 0 0 0 0 0) (0 0 0 0 0 0 0 0))
((0 0 0 0 0 0 0 0) (0 0 0 0 0 0 0 0) (0 0 0 0 0 0 0 0))
((0 0 0 0 0 0 0 0) (0 0 0 0 0 0 0 0) (0 0 0 0 0 0 0 0))
((0 0 0 0 0 0 0 0) (0 0 0 0 0 0 0 0) (0 0 0 0 0 0 0 0)))}
Or bind results of Lisp invocations like this:
(%i21) :lisp (msetq $foo (make-array '(4 3 8)));
#3A(((NIL NIL NIL NIL NIL NIL NIL NIL)
(NIL NIL NIL NIL NIL NIL NIL NIL)
(NIL NIL NIL NIL NIL NIL NIL NIL))
((NIL NIL NIL NIL NIL NIL NIL NIL)
(NIL NIL NIL NIL NIL NIL NIL NIL)
(NIL NIL NIL NIL NIL NIL NIL NIL))
((NIL NIL NIL NIL NIL NIL NIL NIL)
(NIL NIL NIL NIL NIL NIL NIL NIL)
(NIL NIL NIL NIL NIL NIL NIL NIL))
((NIL NIL NIL NIL NIL NIL NIL NIL)
(NIL NIL NIL NIL NIL NIL NIL NIL)
(NIL NIL NIL NIL NIL NIL NIL NIL)))
(%i21) foo;
(%o21) {Array: #3A(((NIL NIL NIL NIL NIL NIL NIL NIL)
(NIL NIL NIL NIL NIL NIL NIL NIL)
(NIL NIL NIL NIL NIL NIL NIL NIL))
((NIL NIL NIL NIL NIL NIL NIL NIL)
(NIL NIL NIL NIL NIL NIL NIL NIL)
(NIL NIL NIL NIL NIL NIL NIL NIL))
((NIL NIL NIL NIL NIL NIL NIL NIL)
(NIL NIL NIL NIL NIL NIL NIL NIL)
(NIL NIL NIL NIL NIL NIL NIL NIL))
((NIL NIL NIL NIL NIL NIL NIL NIL)
(NIL NIL NIL NIL NIL NIL NIL NIL)
(NIL NIL NIL NIL NIL NIL NIL NIL)))}
By the way, array probably worked for you, too. I have never used it and was confused at first, since it is not printed after creation. But after checking the documentation and a Wikibooks article:
(%i22) array(A,2,2,2);
(%o22) A
(%i23) arrayinfo(A);
(%o23) [declared, 3, [2, 2, 2]]
(%i24) A[0,1,2]: 2;
(%o24) 2
(%i25) listarray(A);
(%o25) [#####, #####, #####, #####, #####, 2, #####, #####, #####, #####,
#####, #####, #####, #####, #####, #####, #####, #####, #####, #####, #####,
#####, #####, #####, #####, #####, #####]
There seem to be quite a few options for this kind of thing in Maxima, or, as the above linked Wikibooks article quotes Robert Dodier: "Maxima's current array/matrix semantics are a mess […]"

Related

More than one argument for predicate in delete-if

Suppose I want to delete one step (element) from the states list.
(defparameter *states* '((:top nil nil) (:subjects nil nil)))
;predicate
(defun equal-state? (step state)
(equal (car step) state))
If I use (delete-if #'equal-state? *states*) then how the second argument ( state) can be passed to predicate?
edited: I have finally found a similar question but I am inclined to retain it because of clarity in the question.
CL-USER 67 > (let ((state :top))
(delete-if (lambda (step)
(equal-state? step state))
*states*))
((:SUBJECTS NIL NIL))
or
CL-USER 68 > (defun make-predicate (state)
(lambda (step)
(equal-state? step state)))
MAKE-PREDICATE
CL-USER 69 > (delete-if (make-predicate :subjects)
*states*)
((:TOP NIL NIL))
As user coredump mentions, delete-if is a potentially destructive operation. The non-destructive alternative to delete-if is remove-if.
One can also use remove/ delete:
CL-USER 77 > (remove :subjects
'((:top nil nil)
(:subjects nil nil))
:key #'first
:test #'equal)
((:TOP NIL NIL))

Lisp (null (QUOTE NIL)) returns NIL

I am new to lisp and I'm trying to a simple check to see if a list is empty. For test purposes i created this test function:
(defun test (list)
(if (null (caddr list))
(make-node 1)
(caddr list)))
if uses the make-node function defined as:
(defun make-node (atm)
(cons atm `(`() `())))
when running (make-node 6) I get:
(6 (QUOTE NIL) (QUOTE NIL))
which is what I want.
i then call (test (make-node 6)) which i then get:
(QUOTE NIL)
which is the (caddr list) from test. if you run (null (QUOTE NIL)) you get T which is what i want to get but when i run it from the given test function i receive NIL.
So my question is why when i check if this is null why do i get NIL instead of T?
When you evaluate (null (quote nil)), (quote nil) is evaluated, resulting in nil being used as the argument for the function null.
However when you evaluate something like (null (function-returning-quote-nil)), function-returning-quote-nil is evaluated, resulting in the list (quote nil) which is then used as the argument for the function null without further evaluation.
Compare to the difference between
(null (quote nil)) ; => t
and
(null '(quote nil)) ; => nil
(CADDR '(6 (QUOTE NIL) (QUOTE NIL))) ; ==> (QUOTE NIL) or just 'NIL
The list with the two symbols QUOTE and NIL are not equal to NIL. Only NIL is equal to NIL. eg.
(DEFPARAMETER TEST 'NIL)
TEST ; ==> NIL
(NULL TEST) ; ==> T
This works since 'NIL gets evaluated to NIL and assigned to TEST. TEST gets evaluated to NIL and it's CLs NULL value. However:
(DEFPARAMETER TEST2 ''NIL)
TEST2 ; ==> 'NIL or (QUOTE NIL)
(NULL TEST2) ; ==> NIL
A list with two elements QUOTE and NIL, famously displayed as 'NIL, is NOT NIL. Only NIL is NIL.
EDIT
After looking at your comments about unquoting I'm pretty sure you want this as make-node:
(defun make-node (atm)
(cons atm '(() ())))
(test (make-node 6)) ; ==> (1 NIL NIL)
There is no such thing as unquoting in a data structure and unless you actually want the symbol quote in your data there is no sense in having quotes inside something quoted. (Half truth since there is, but it involves macros)

LISP modify list passed as argument

So i got this function that receives a list as an argument
(defun do-transf (lst)
...
my initial lst is ((NIL NIL) (NIL NIL))
but in the end i want the lst becomes
(NIL NIL NIL NIL)
example
(defvar a (list (list NIL NIL) (list NIL NIL)))
(do-transf a)
(NIL NIL NIL NIL) -> gives the right answer but when i call the list this isnt the answer
a
((NIL NIL) (NIL NIL)) -> i dont want this
Your post suggests that you wish to do list concatenation, i.e. append lists to another.
As such, try this:
(defvar a (list (list NIL NIL) (list NIL NIL)))
(apply #'append a)
This outputs:
(NIL NIL NIL NIL)
Short, simple, sweet.
UPDATE:
Since you want destructive behavior (that is, modification of the original value of a):
(setq a (apply #'append a))
Now, a is set to this new value (from ((NIL NIL) (NIL NIL))):
(NIL NIL NIL NIL)
Typically, lisp functions don't mutate the data they operate on. This is not a required property, but it does make it much easier writing and reasoning about large code bases.
So, what typically happens is that a function will return a copy of the new data, that can then replace the old value in a variable, at the discretion of the caller.
Even when functions destructively modify data structures, they tend to return the new resulting value.
If we look at the following code:
(defvar *example* '(delete 2 3 4))
=> *example*
(delete 'delete *example*)
=> (2 3 4)
*example*
=> (delete 2 3 4)
This is because there is no way for the function to destructively modify *example* so it loses the first element, so even when you are using destructive, mutating functions, they need to return a value suitable for storing back into the variable that the input came from.
Try using flatten..
(defvar a (flatten (list (list NIL NIL) (list NIL NIL))))

Why does SBCL eval function lose the macrolet it's running in?

(print x) prints exactly what I want to eval, but (eval x) fails,
but if I run x it works! What am I missing?
Please tell me why this doesn't work, or if I'm doing something stupid.
I'm trying to print a table of dynamic size and setting lambda variables to eventually evaluate an expression for each cell in the table.
BTW I figured out why eval fails. (eval x) is losing the macrolet, but WHY?!
This works:
(defvar varlist '(a b c d))
(defvar vvars '(c d))
(defvar hvars '(a b))
(macrolet ((AlternateVariable (var &rest body)
`(dolist (,var '(nil t)) ,#body))
(AlternateVariables (varlist &rest body)
(if (null varlist)
(cons 'progn body)
`(AlternateVariable ,(car varlist)
(AlternateVariables ,(cdr varlist) ,#body)))))
(let ((listvarlist (cons 'list varlist)))
(print
`(AlternateVariables ,(reverse vvars)
(AlternateVariables ,(reverse hvars)
(format t "row=~S~%" ,listvarlist) ))))
nil)
and it prints what I want it to:
(alternatevariables (d c)
(alternatevariables (b a) (format t "row=~S~%" (list a b c d))))
If I change that "print" to "eval" I get
; in: alternatevariables (d c)
; (B A)
; caught warning:
; undefined variable: a
but if I run what it printed (inside the macrolet) it works!
(macrolet ((AlternateVariable (var &rest body)
`(dolist (,var '(nil t)) ,#body))
(AlternateVariables (varlist &rest body)
(if (null varlist)
(cons 'progn body)
`(AlternateVariable ,(car varlist)
(AlternateVariables ,(cdr varlist) ,#body)))))
(alternatevariables (d c)
(alternatevariables (b a) (format t "row=~S~%" (list a b c d))))
nil)
and it prints
row=(nil nil nil nil)
row=(t nil nil nil)
row=(nil t nil nil)
row=(t t nil nil)
row=(nil nil t nil)
row=(t nil t nil)
row=(nil t t nil)
row=(t t t nil)
row=(nil nil nil t)
row=(t nil nil t)
row=(nil t nil t)
row=(t t nil t)
row=(nil nil t t)
row=(t nil t t)
row=(nil t t t)
row=(t t t t)
nil
I have heard "eval is evil" but I need the backquote to get the right evaluation order and evaluate some arguments but not others. Print is a good debug tool, but then I'm missing something with eval. Does eval lose the macrolet?
! That's it. I defined those two macrolet macros as defmacros and then eval works!
Why?! Eval is losing the macrolet. (I'm new to macrolet and no lisp genius)
Or, how do I do this without eval?
Any general notes on my code would be nice too. Is this over complicated?
Also, errors in macros seem to get awkward error messages. Any help with that?
$ sbcl
This is SBCL 1.1.2-1.fc18, an implementation of ANSI Common Lisp.
EVAL in Common Lisp only evaluates in a current dynamic environment and the null lexical environment. Enclosing it in lexical constructs like MACROLET, LET, ... or similar is not going to work.

Lisp: Why and how does '(nil nil) evaluate to true?

(if '(nil nil)
'print-true
'print-false)
(if '(nil)
'print-true
'print-false)
In the code above, why does the Lisp interpreter always evaluate these forms to true (print-true). I thought nil represented false in Common Lisp.
I am using GNU CLISP.
nil is false. Anything else is true. '(nil) is a list with one element, namely nil. '(nil nil) is a list with two elements, namely nil and nil. Neither of these expressions is the same as nil by itself, so if sees it as true.
nil is equivalent to an empty list.
CL-USER> (if (list ) 'print-true 'print-false)
; prints PRINT-FALSE
CL-USER> (if (list nil) 'print-true 'print-false)
; prints PRINT-TRUE
'(nil) is equiv to (list nil) which is different from an empty list.