我在SML中编写作业,我做了一些练习题,我觉得我错过了一些东西 - 我觉得我使用了太多的case
陈述。下面是我在做什么,问题语句我遇到:案例陈述和模式匹配
什么麻烦写一个函数all_except_option,这需要一个字符串,字符串列表。如果字符串不在列表中,则返回NONE,否则返回SOME lst,其中lst与参数列表类似,但字符串不在其中。
fun all_except_option(str : string, lst : string list) = case lst of [] => NONE | x::xs => case same_string(x, str) of true => SOME xs | false => case all_except_option(str, xs) of NONE => NONE | SOME y=> SOME (x::y)
写一个函数get_substitutions1,这需要一个字符串列表清单(字符串,换人名单列表)和一个字符串s,并返回一个字符串列表。结果中的所有字符串在某些列表中也有s,但s本身不应该在结果中。
fun get_substitutions1(lst : string list list, s : string) = case lst of [] => [] | x::xs => case all_except_option(s, x) of NONE => get_substitutions1(xs, s) | SOME y => y @ get_substitutions1(xs, s)
- same_string
是所提供的功能,所有的 fun same_string(s1 : string, s2 : string) = s1 = s2
这是Coursera编程语言课程第2周的作业的一部分。由于这是在线发布解决方案的违规行为,因此我要求将此问题重新措辞以更改函数名称,使其与赋值完全不匹配。 – arnab