Quantcast
Channel: MaplePrimes - Questions and Posts tagged with goto

implementing switch/case?

$
0
0

Is there a switch/case statement in Maple?  So that instead of:

 

if (a = a1) then

   b1

elif (a = a2) then

   b2

elif (a = a3) then

  b3

end if

 

Something like:

 

switch (a) {

case: a1 {b1}

case: a2 {b2}

case: a3 {b3}

}

 

???

 

I'm potentially going to have a long list of elif's and might need  more efficient way of doing it.


is there a conflict - using a goto statement and profiling?

$
0
0

Hi everybody,

I got an error message, when i try to profile code with a goto statement inside.
Here is my minimal-example in Maple18. Is there a known conflict or what's wrong?

f := proc(x)
    if x < 0 then goto(POS): end if;
    return "A";
    
    POS:
    return "B";
end proc:

f(-3);
                              "B"
f(3);
                              "A"

CodeTools[Profiling]:-Profile(f);
f(-3);
CodeTools[Profiling]:-PrintProfiles(f);
CodeTools[Profiling]:-UnProfile(f);

Error, (in f) goto to an undefined or unreachable label
f
f := proc(x)
     |Calls Seconds  Words|
PROC |    1   0.000      3|
      1 |    1   0.000      3| if x < 0 then
      2 |    1   0.000      0|   goto(POS)
                               end if;
      3 |    0   0.000      0| return "A";
      4 |    0   0.000      0| POS;
      5 |    0   0.000      0| return "B"
end proc

how to have a common exit point in a proc?

$
0
0

Maple does not have a GOTO. I do ?goto and nothing comes up (I thought it did at one point, but it seems to be gone). I also saw 

99052-How-To-Write-Procedures-That-Use-Go-To-In-Maple

GOTO is considered bad, and I agree in general. But there ONE very good use for GOTO which can't be easily replaced, which is having a common exit label. (at one work place sometime ago, this was actually the only recommened use for it in the programming guidelines. and I agree.)

Without this, one ends up with deep if then else if then else if then else., etc....

But having a common exit, where one can do common clean up things is very good. This reduced code duplication and actually makes the logic more clear.

Here is just some silly example to  illustrate

foo:=proc(x)
  if x=10 then
     .....
    close file, print common message
    return(final_result);
  fi;

  if x=12 then
     .....
    close file, print common message
    return(final_result);
  fi;

 etc...

end proc;

With common exit point, one could do

foo:=proc(x)
   if x=10 then 
      .....
     final_result :=...
     goto common_exit;
   fi;

   if x=12 then 
      .....
     final_result :=...
     goto common_exit;
   fi;

common_exit:
   close file; 
   print common message;
   return(final_result);

end proc;

The alternative is to have deep  nested if then else, like this

foo:=proc(x)
    if x=10 then 
       ..... 
       final_result :=...;
    else
         if x=12 then 
            ..... 
            final_result :=...;
         else
             if x= 20 then
                .....
                final_result :=...;
             fi;
         fi;
   fi;

  close file; 
  print common message;
 return(final_result);
end proc;

For complicated logic, I do not think the last case is better than the second one using GOTO.

The second case also eliminates duplicate code as was done in first case. Also first case has multiple return points from the proc, which is not good. As having one common return point is better.

Why was goto removed from Maple? Can one still use it somehow? Where is the documenation for it?

update

 

goto seems to be there. I did not think of trying, since ?goto did not show it. But now I found I could do this and it works

foo:=proc()
  local x;
  x:=10;
  goto(common_exit);
  x:=20;
common_exit:
  print(x);
end proc;

So please Maplesoft., do not remove GOTO.

How do I run coding with goto on Maple?

$
0
0

Hi, I'm using Maple 2018 and I tried to run coding from https://www.maplesoft.com/applications/view.aspx?sid=4194&view=html

however, it said : unable to parse. I figured out that the problem maybe is in the if loop. though it seems perfectly fine, but it has some goto commands that i cannot search on maple website. does this mean that the goto cannot be used here and should be replaced? if yes, then how? 

i am still learning on how to use maple. any help would be much appreciated. thank you!

this is the coding for if loop:

 

label_7;

rv:=vector([p1(x1pt,x2pt),p2(x1pt,x2pt)]):

numgeval:=numgeval+1;

printf("%5d (%8.4f,%8.4f)",numIter,rv[1],rv[2]);

max:=n;

mg:=convert(sqrt(dotprod(rv,rv)),float);

printf("%12.4f",mg);

if(mg<tol or numIter>=max) then

goto(label_6);

else

numIter:=numIter+1;

fi;

v1:=x1pt+t*rv[1];

v2:=x2pt+t*rv[2];

newt:=evalf(subs({x1=v1,x2=v2},f1));

numfeval:=numfeval+1;

lam:=fsolve(diff(newt,t)=0,t,maxsols=1);

nv1:=evalf(subs({t=lam},v1));

nv2:=evalf(subs({t=lam},v2));

printf(" (%8.4f,%8.4f)%13.4f\n",x1pt,x2pt,lam);

x1pt:=nv1;

x2pt:=nv2;

goto(label_7);

label_6;

printf("\n\n-----------------------------------------");

printf("---------------------------------------------");

printf("\n\n Approximate Solution: ");

printf(" (%8.4f,%8.4f)\n",x1pt,x2pt);

Fvalue:=evalf(subs(x1=x1pt,x2=x2pt,f));

printf(" Maximum Functional Value: ");

printf("%21.4f",Fvalue);

printf("\n Number gradient evaluations:");

printf("%22d",numgeval);

printf("\n Number function evaluations:");

printf("%22d",numfeval);

printf("\n\n-----------------------------------------");

printf("---------------------------------------------");

end:





Latest Images