编写一个函数remove_option,它接受一个字符串和一个字符串列表.如果字符串不在列表中,则返回NONE,否则返回SOME xs,其中xs与参数列表相同,但字符串不在其中.您可以假设字符串最多只在列表中一次.使用提供给您的same_string来比较字符串.样品溶液约为8行.
函数类型应该是fn:string*string list - > string list option.Here是我的代码
fun same_string(s1 : string, s2 : string) =
s1 = s2
fun remove_option (str: string ,str_list : string list) =
case str_list of
[] => NONE
| x::xs => if same_string(x,str)
then SOME xs
else x :: remove_option( str,xs)
Run Code Online (Sandbox Code Playgroud)
和错误报告
hw2provided.sml:10.5-15.37 Error: right-hand-side of clause doesn't agree with f
unction result type [tycon mismatch]
expression: _ option
result type: string list
in declaration:
remove_option =
(fn (<pat> : string,<pat> : string list) =>
(case str_list
of <pat> => <exp>
| <pat> => <exp>))
uncaught exception Error
raised at: ../compiler/TopLevel/interact/evalloop.sml:66.19-66.27
../compiler/TopLevel/interact/evalloop.sml:44.55
../compiler/TopLevel/interact/evalloop.sml:292.17-292.20
Run Code Online (Sandbox Code Playgroud)
那么bug在哪里?
问题是你要返回一条string list option但是该线
else x :: remove_option( str,xs)
Run Code Online (Sandbox Code Playgroud)
看起来你想要回归一个 string list
你需要做的是返回值remove_option( str,xs)是什么
1)决定如果是这样做的 NONE
2)提取字符串列表strings(或任何你想要调用的字符串),如果它是表单SOME strings,x粘贴到列表的前面,并SOME在返回之前重新打包它.
你似乎对使用感到满意case,所以你可以在这里使用它.