【发布时间】:2013-11-05 11:12:03
【问题描述】:
我想做一个函数,它接受一个字符串列表和一个字符串,如果字符串列表中没有字符串,则返回NONE,否则返回与原始字符串相同的字符串列表的SOME字符串列表,但不包含初始字符串(模式):
fun my_function (pattern, source_list) =
case source_list
of [] => NONE
| [x] => if pattern = x then SOME [] else NONE
| x::xs =>
if pattern = x
then SOME (xs)
else SOME (x) :: my_function (pattern, xs) (* this is wrong, what to do here?*)
val a = my_function ("haha", ["12", "aaa", "bbb", "haha", "ccc", "ddd"]) (* should be SOME ["12", "aaa", "bbb", "ccc", "ddd"]*)
val a2 = my_function ("haha2", ["123", "aaa", "bbb", "haha", "ccc"]) (*should be NONE*)
val a3 = my_function ("haha3", ["haha3"]) (* should be SOME []*)
我对第三种情况感到困惑:x::xs => .... 那应该怎么做?
请注意,我不想使用任何 sml 库函数。
【问题讨论】:
-
如果模式出现多次怎么办?是否应该全部删除?
-
@tahatmat,假设它只能发生一次。
标签: recursion functional-programming sml