這是怎麼回事就已經解決方案:
(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