Ending a procedure early in Maple

240 Views Asked by At

I want to jump to the end of the procedure if an IF statement is true

NzdOrInv := proc (a::integer, m::integer) 
if (a > m) then 
   printf("a > m, cannot proceed. Terminating program.") <-- I WANT TO JUMP TO THE END OF THE PROCEDURE HERE
end if 
if gcd(a, m) = 1 then 
    inverseCalc(a, m) 
elif gcd(a, m) > 1 then 
    compZeroDiv(a, m) 
end if 
end proc

I've tried inserting a end proc after the line printf("a > m, cannot proceed. Terminating program.") but it doesn't work.

2

There are 2 best solutions below

0
On BEST ANSWER

Here are three ways.

I realize that first way below may be closest to what you actually asked. But the third way has an advantage in that a error emitted by the failed attempt can be caught and trapped. This allows you to programmatically determine what Maple might do next, according to whether it failed or not. In other words, in gives you easier control over the overall program flow.

restart:

NzdOrInv := proc(a::integer, m::integer)
   if (a > m) then
      printf("a > m, cannot proceed. Terminating program.");
      return NULL;
   end if;
   if gcd(a, m) = 1 then
       inverseCalc(a, m);
   elif gcd(a, m) > 1 then
       compZeroDiv(a, m);
   end if;
   end proc:

NzdOrInv( 5, 3 );
            a > m, cannot proceed. Terminating program.

restart:

NzdOrInv := proc(a::integer, m::integer)
   if (a > m) then
      printf("a > m, cannot proceed. Terminating program.");
   elif gcd(a, m) = 1 then
      inverseCalc(a, m);
   elif gcd(a, m) > 1 then
      compZeroDiv(a, m);
   end if;
end proc:

NzdOrInv( 5, 3 );
            a > m, cannot proceed. Terminating program.

restart:

NzdOrInv := proc(a::integer, m::integer)
   if (a > m) then
      error "a > m, cannot proceed.";
   elif gcd(a, m) = 1 then
      inverseCalc(a, m);
   elif gcd(a, m) > 1 then
      compZeroDiv(a, m);
   end if;
end proc:

NzdOrInv( 5, 3 );
Error, (in NzdOrInv) a > m, cannot proceed.

try
   NzdOrInv( 5, 3 );
catch "a > m, cannot proceed":
   print("some new calculation...");
end try;
                      "some new calculation..."
0
On

A fourth way is to let Maple's excellent argument-processing facility handle it.

N:= proc(a::integer, m::And(integer, satisfies(m-> a <= m)))
     #body of procedure
end proc;