[] | x::[]-6ren">
gpt4 book ai didi

pattern-matching - SML 模式匹配抛出 "types of rules don' t 同意 [tycon 不匹配]"错误

转载 作者:行者123 更新时间:2023-12-02 19:34:32 27 4
gpt4 key购买 nike

fun sample(list_of_str_lists, s) =
case list_of_str_lists of
[] => []
| x::[] => case (all_except_option(s, x)) of
SOME lst => lst
| NONE => []
| x::xs' => case (all_except_option(s, x)) of
SOME lst => lst @ sample(xs', s)
| NONE => [] @ sample(xs', s)

它使用一个辅助函数,该函数接受一个字符串列表,如果该列表中有匹配的字符串(同样,不包括匹配的字符串),则在列表选项中返回该列表中的所有元素。因此,辅助函数将采用一个列表 ["a", "b", "c"] 和一个字符串 "a" ,如果它匹配 "a ",将返回一个包含 ["b", "c"] 的选项。

我的问题出在最后一个模式匹配分支的sample中。它抛出错误

earlier rule(s): 'Z list option -> 'Z list
this rule: string list list -> string list
in rule:
:: (x,xs') =>
(case (all_except_option (s,x))
of SOME lst => lst @ sample <exp>
| NONE => nil @ sample <exp>

但我不明白为什么,因为我尝试匹配的模式调用SOME lst,但它将其描述为字符串列表列表。这是怎么回事?

最佳答案

这是由于嵌套 case 表达式而导致的语法问题。解析器假设情况 | x::xs' => ... 是正在进行的 case 表达式 case (all_ except_option(s, x)) of ... 的延续,因为它无法判断它是实际上属于封闭表达式 case list_of_str_lists of ...

如果将内部 case 表达式括在括号中,函数定义将进行解析。

fun sample(list_of_str_lists, s) =
case list_of_str_lists of
[] => []
| x::[] => (case (all_except_option(s, x)) of
SOME lst => lst
| NONE => [])
| x::xs' => (case (all_except_option(s, x)) of
SOME lst => lst @ sample(xs', s)
| NONE => [] @ sample(xs', s))

关于pattern-matching - SML 模式匹配抛出 "types of rules don' t 同意 [tycon 不匹配]"错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26724309/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com