【发布时间】:2020-07-30 16:36:00
【问题描述】:
我有下面的代码,但我对它为什么不编译感到困惑。我知道模式匹配可以用于匹配不同类型的构造函数,只要右侧的表达式返回相同的类型,就可以了。但这不会在下面发生。有人可以解释一下吗?
fun myfunc (s: string, lst: string list) =
let fun f (s, []) = NONE
| f (s, x::xs') = SOME ["list"]
| f (s, SOME(x::xs')) = SOME ["some"]
in
f(s, lst)
end
在尝试运行此代码时,编译器会抛出错误说明
hw2provided.sml:25.13-27.46 Error: parameter or result constraints of clauses don't agree [tycon mismatch]
this clause: 'Z * 'Y list option -> 'X
previous clauses: 'Z * 'W list -> 'X
in declaration:
filter =
(fn (s,nil) => NONE
| (s,:: <pat>) => SOME (<exp> :: <exp>)
| (s,SOME <pat>) => SOME (<exp> :: <exp>))
【问题讨论】:
-
尝试找出
f的类型——它是'a * T -> string list option,对于某些类型T。什么是T?
标签: pattern-matching sml ml