Name:

proc defines a Sollya procedure

Usage:

proc(formal parameter1, formal parameter2,..., formal parameter n) { procedure body } : void -> procedure proc(formal parameter1, formal parameter2,..., formal parameter n) { procedure body return expression; } : void -> procedure proc(formal list parameter = ...) { procedure body } : void -> procedure proc(formal list parameter = ...) { procedure body return expression; } : void -> procedure

Parameters:

Description:

Example 1:

   > succ = proc(n) { return n + 1; };
   > succ(5);
   6
   > 3 + succ(0);
   4
   > succ;
   proc(n)
   {
   nop;
   return (n) + (1);
   }

Example 2:

   > add = proc(m,n) { var res; res := m + n; return res; };
   > add(5,6);
   11
   > add;
   proc(m, n)
   {
   var res;
   res := (m) + (n);
   return res;
   }
   > verbosity = 1!;
   > add(3);
   Warning: at least one of the given expressions or a subexpression is not correctly typed
   or its evaluation has failed because of some error on a side-effect.
   error
   > add(true,false);
   Warning: at least one of the given expressions or a subexpression is not correctly typed
   or its evaluation has failed because of some error on a side-effect.
   Warning: the given expression or command could not be handled.
   error

Example 3:

   > succ = proc(n) { return n + 1; };
   > succ(5);
   6
   > succ(x);
   1 + x

Example 4:

   > hey = proc() { print("Hello world."); };
   > hey();
   Hello world.
   > print(hey());
   Hello world.
   void
   > hey;
   proc()
   {
   print("Hello world.");
   return void;
   }

Example 5:

   > fac = proc(n) { var res; if (n == 0) then res := 1 else res := n * fac(n - 1); return res; };
   > fac(5);
   120
   > fac(11);
   39916800
   > fac;
   proc(n)
   {
   var res;
   if (n) == (0) then
   res := 1
   else
   res := (n) * (fac((n) - (1)));
   return res;
   }

Example 6:

   > myprocs = [| proc(m,n) { return m + n; }, proc(m,n) { return m - n; } |];
   > (myprocs[0])(5,6);
   11
   > (myprocs[1])(5,6);
   -1
   > succ = proc(n) { return n + 1; };
   > pred = proc(n) { return n - 1; };
   > applier = proc(p,n) { return p(n); };
   > applier(succ,5);
   6
   > applier(pred,5);
   4

Example 7:

   > verbosity = 1!;
   > myquit = proc(n) { print(n); quit; };
   > myquit;
   proc(n)
   {
   print(n);
   quit;
   return void;
   }
   > myquit(5);
   Warning: a quit or restart command may not be part of a procedure body.
   The procedure will not be executed.
   Warning: an error occurred while executing a procedure.
   Warning: the given expression or command could not be handled.
   error

Example 8:

   > printsucc = proc(n) { var succ; succ = proc(n) { return n + 1; }; print("Successor of",n,"is",succ(n)); };
   > printsucc(5);
   Successor of 5 is 6

Example 9:

   > makeadd = proc(n) { var add; print("n =",n); add = proc(m,n) { return n + m; }; return add; };
   > makeadd(4);
   n = 4
   proc(m, n)
   {
   nop;
   return (n) + (m);
   }
   > (makeadd(4))(5,6);
   n = 4
   11

Example 10:

   > sumall = proc(L = ...) { var acc, i; acc = 0; for i in L do acc = acc + i; return acc; };
   > sumall;
   proc(L = ...)
   {
   var acc, i;
   acc = 0;
   for i in L do
   acc = (acc) + (i);
   return acc;
   }
   > sumall();
   0
   > sumall(2);
   2
   > sumall(2,5);
   7
   > sumall(2,5,7,9,16);
   39
   > sumall @ [|1,...,10|];
   55
See also: return, externalproc, void, quit, restart, var, @, bind, getbacktrace, error