Question: why "Error, adding lists of different length" when no print statement?

I can't understand why this error Error, adding lists of different length show up only when removing a print statement!

Why would a print has anything to do with an error message showing up or not?

some context. I wanted to call map on expression, but wanted to collect the result of operating on each indent in a list as map goes through the expression on term after the other. The expression will be type `+`.

I did not know how to do it inside map. So I create separate proc which map call.

Inside this proc, I use list to append to (this will small list, few terms at most).  To be able to do this, I made the list I want to collect thing into a global variable outside the proc.  Everything was working OK, until I remove a print statement  I had inside the proc for debugging.

I am sure there is a better way to do this all (i.e. collect map output into a list) and I am trying to find better way. But my question is: Why would this error shows up only when I remove a print statement from the last line in the proc?

I also notice when removing the line variable declaration global L now both version work with no error, but get warning Warning, (in f) `L` is implicitly declared local which is why I added declaration global L in first place.

Maple 2021.2 on windows 10

interface(version);

`Standard Worksheet Interface, Maple 2021.2, Windows 10, November 23 2021 Build ID 1576349`

restart;

r:=2/(x^2+1)+1/(x^2+1)^2;
L:=[];
f:=proc(Z)
   global L;  
   #do some processing on Z, then collect it into list L
   L:=[ op(L), Z^2];
   #print("op(L) = ",op(L)," Z=",Z);   
end proc:
map(Z->f(Z),r);
L;

2/(x^2+1)+1/(x^2+1)^2

[]

Error, adding lists of different length

[4/(x^2+1)^2, 1/(x^2+1)^4]

restart;

r:=2/(x^2+1)+1/(x^2+1)^2;
L:=[];
f:=proc(Z)
   global L;  
   #do some processing on Z, then collect it into list L
   L:=[ op(L), Z^2];
   print("op(L) = ",op(L)," Z=",Z);   
end proc:
map(Z->f(Z),r);
L;

2/(x^2+1)+1/(x^2+1)^2

[]

"op(L) = ", 4/(x^2+1)^2, " Z=", 2/(x^2+1)

"op(L) = ", 4/(x^2+1)^2, 1/(x^2+1)^4, " Z=", 1/(x^2+1)^2

[4/(x^2+1)^2, 1/(x^2+1)^4]

 

Download why_error.mw

 

Thanks to everyone for the answers and information I did not know that map does process return value from a proc it calls. Good to know.

 

Please Wait...