【发布时间】:2022-01-21 03:33:50
【问题描述】:
我目前正在用 SML 编写一个代码,它接收一个包含数字和空格的字符串列表。代码必须将数字组合到空格,然后为下一组数字创建一个新的列表项,依此类推。
例如,["1", "", "2", "3", "", "4", "5", "6"] 列表将返回["1", "23", "456"] 列表。
我当前的非错误尝试包括以下代码。
fun conc(L) =
case L of
[] => ""
| x::xs => x ^ conc(xs);
fun intParse(L as x::xs) =
let
val intStr = conc(L)
in
intStr :: intParse(xs)
end;
我想写类似下面的代码,但不能没有错误。这不是确切的代码,更像是我无法弄清楚的伪代码。
fun strToInt(nil) = []
| strToInt(L) =
let
fun conc(y::ys) =
if hd(ys) <> "" then y ^ conc(ys)
else y ^ ""
in
conc(L) :: strToInt(xs)
end;
【问题讨论】:
标签: recursion concatenation sml smlnj