1

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.

Pankaj Sejwal
  • 1,605
  • 1
  • 18
  • 27

1 Answers1

2

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)))}
Robert Dodier
  • 16,905
  • 2
  • 31
  • 48
  • this seems to work just fine and I am thankful for this suggestion as well. But I was just wondering too if the way I was doing it is feasible. – Pankaj Sejwal Mar 06 '14 at 04:00
  • 1
    @Rorschach in the result that you got, you can write `ev(%, nouns)` to evaluate the `make_array` noun expression. – Robert Dodier Mar 06 '14 at 17:38