lispcommon-lispmaximacomputer-algebra-systems

Maxima: Returning function from function and evaluating it aftewards


I want to create a function in Maxima similar to ConstantArray in Mathematica. So I tried putting a wrapper around make-array but make_array(fixnum,2,3,4) takes last arguments as a sequence of parameters rather than a list while if one calls ConstantArray(a,b,c,d..) with variable number of arguments than I cannot pass it to make-array without putting it all as list.

To get around the problem of extracting elements from list passed as parameter and putting in make-array function, I tried,

    constantarray((l)):=block([eq:'make_array(fixnum)],
                        map(lambda([x],eq:endcons(x,eq)),l),eq);

which on calling

constantarray([1,2,3,5,3]); 

returns

make_array(fixnum,1,2,3,5,3)

This function executes well if I copy this output, paste it on console and run it as it returns me the Lisp array [1,2,3,5,3].

I have tried evaluating it using ''% & ev(constantarray(1,2,3,5,3),nouns) etc but its just not working. I want to know if someone knows how to force this evaluation or I am doing something not possible.


Solution

  • Try this.

    constant_array ([L]) := block ([x : first (L), d : rest (L)],
        apply (make_array, cons ('any, d)),
        fillarray (%%, [x]));
    

    A function definition foo([L]) := ... means that the function takes a variable number of arguments, and L is the list of arguments actually supplied. apply (make_array, cons ('any, d)) is like calling make_array('any, d[1], d[2], d[3], ...). Also, in a block, %% is the value of the previous expression.

    Example:

    constant_array (1234, 4, 3, 2);
      => {Lisp Array: 
        #3A(((1234 1234) (1234 1234) (1234 1234))
            ((1234 1234) (1234 1234) (1234 1234))
            ((1234 1234) (1234 1234) (1234 1234))
            ((1234 1234) (1234 1234) (1234 1234)))}