【发布时间】:2017-07-16 20:30:40
【问题描述】:
我正在编写一个递归 ML 函数,它接受一个字符串和一个索引值,并在给定索引处拆分字符串。该函数应返回一个包含两个字符串的列表。
我知道我需要两种基本情况,一种检查是否已达到索引,另一种检查字符串是否没有字符。我坚持如何将字符分配给不同的字符串。注意,我使用了一个辅助函数来清理初始调用,这样就不需要在每个函数调用上都键入explode。
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"];
【问题讨论】:
标签: list recursion sml smlnj ml