我想创建一个函数,它接受一个字符串列表和一个字符串,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 库函数。