Search code examples
maxima

Assigning return to variable dosent work


I have an function which generate list with random numbers (with return statement). After I wanted to assign result of that function to variable.

something like that:

function() := (
     list:[5,2,5,3,60, 11, -5],
     return(list)
);

list2 : function();

but result of function dosent assign to list2. Where's the problem?


Solution

  • In maxima is not needed to add the retrun code to a function, it will return the last line, and also it is not necessary to assing the list a variable:

    (%i19) my_function() := (
         [5,2,5,3,60, 11, -5]
    );
    
    list2 : my_function();
    

    If you want to use return you can encapsulate the code in a block:

    (%i21) function() := (
         block(list:[5,2,5,3,60, 11, -5],
               return(list))
    );
    

    Then It should work, you have also the random function to generate random numbers:

    makelist(random(10),10);
    

    for example this generates a list with ten random integers from 0 to 9