Function definition

Programs are stored in executable arrays and the curly brackets generate executable arrays. For example, if you input the line

    {  add 2 mul }
then the executable array object which represents the program ``take two elements from the stack, add them, and multiply two and put the result on the stack'' will be store on the top of the stack. You can bind the program to a name. That is,
   /abc { add 2 mul } def
binds the executable array to the variable abc. The input 2 4 abc :: outputs 12. When sm1 encounters the name abc, it looks up the user dictionary and finds that the value of abc is the executable array { add 2 mul } . The executable array is loaded to the stack machine and executed.

Funtions can be defined by using executable arrays. Here is a complete example of a function definition in sm1 following standard conventions.

   /foo {
     /arg1 set
     [/n /i /ans] pushVariables
     [
       /n arg1 def
       /ans 0 def
       1 1 n {
         /i set
         /ans ans i add def
       } for
       ans /arg1 set
     ] pop
     popVariables
     arg1
   } def
The function returns the sum $1+2+\cdots+ n$. For example, 100 foo :: outputs $5050$. The arguments of the function should firstly be stored in the variables arg1, arg2, $\ldots$. It is a convension in sm1 programming. The local variables are declared in the line
     [/n /i /ans] pushVariables
The macro pushVariables stores the previous values of n, i, ans on the stack and the macro popVariables restores the previous values. So, you can use n, i, ans as a local variable of this function. The function body should be enclosed as
    [

    ] pop
It is also a convension in sm1 programming to avoid unmatched use of pushVariables and popVariables.

Example 12   cv0.sm1 is a script to compute characteristic variety for $D$-submodules in $D^n$.

cv2.sm1 is a script to compute the multiplicites of $D$-modules.

Nobuki Takayama 2020-11-24