1
我正在寫一個遞歸ML函數,它接受一個字符串和一個索引值,並將該字符串分割給定索引。該函數應該返回一個包含兩個字符串的列表。如何將字符串分爲兩部分,遞歸地編寫
據我所知,我需要兩個基本案例來檢查是否已達到索引,另一個檢查字符串是否超出字符。我被困在如何將字符分配給不同的字符串。請注意,我使用了一個輔助函數來清除最初的調用,這樣在每次函數調用時都不需要輸入爆炸。
fun spliatHelp(S, num) =
if null S then nil
else if num = 0 then hd(S) :: (*string2 and call with tl(S)*)
else hd(S) :: (*string1 and call with tl(S)*)
fun spliat(S, num) =
spliatHelp(explode(S), num);
從spliat(「theString」,3)的輸入;
我的理想輸出是[「the」,「String」];