这是怎么回事就已经解决方案:
(define (Cat num1 num2)
(define (CatLoop num1 num2 temp)
(if (= temp 0)
(+ num1 num2)
(CatLoop (* num1 10) num2 (/ temp 10))))
(CatLoop num1 num2 num2))
[它可能包含语法错误,我没有测试。]
在具有嵌套函数的类C语言:
int Cat(int num1, int num2) {
int CatLoop(int num1, int num2, int temp) {
if (temp == 0)
return num1 + num2;
else
return CatLoop(num1 * 10, num2, temp/10);
}
return CatLoop(num1, num2, num2);
}
尾调用优化后,这个被展开到如下:
int Cat(int num1, int num2) {
int temp = num2;
// goto CatLoop;
CatLoop:
if (temp == 0)
goto Done;
Else:
num1 *= 10;
temp /= 10;
goto CatLoop;
Done:
return num1 + num2;
}
如果你要正确对待这些数字作为字符串,然后使用字符串。 “连接”不是**,而是整数。首先,“整数”是一个概念,与数字表示的方式分开存在。不管你写什么基地,或者如果你用13个理货标记来代表它,或者画一个完整的符号,你就会说代表那个价值,“十三”是一样的。 – 2011-01-09 21:17:35
为什么你需要递归编写它?这是功课吗?什么语言?你试过什么了? – templatetypedef 2011-01-09 21:19:20