hermann on Sat, 14 Feb 2026 15:54:20 +0100


[Date Prev] [Date Next] [Thread Prev] [Thread Next] [Date Index] [Thread Index]

Problem with recursive function return value


The return value of if() works as expected:

? print(if(4==5,3,2))
2
? print(if(4==5-1,3,2))
3
?

I want to compute this result recursively:

? chinese(chinese(chinese(Mod(1,2),Mod(2,3)),Mod(2,11)),Mod(3,31))
Mod(1553, 2046)
?

With

? A=[1,2;2,3;2,11;3,31];
? mo(v)=Mod(v[1],v[2]);
? ch(M)=print(M);if(#M==1,mo(A[,1]),chinese(mo(A[,1]),ch(M[,2..#M])));
?

The print() in ch() is only for stone age debugging purposes. And output indicates correct recursive execution, but the returned value is not correct — why?

? ch(A~)
[1, 2, 2, 3; 2, 3, 11, 31]
[2, 2, 3; 3, 11, 31]
[2, 3; 11, 31]
[3; 31]
Mod(1, 2)
?

Regards,

Hermann.