首页 文章

ocaml type missmatch unit vs list

提问于
浏览
0

为此签名

val chooser: string list * string list -> string list

和这个实现

let rec chooser (inputList, trueList) = match inputList with
      [] -> []
    | iH::iT -> if (List.hd trueList)="True" 
        then iH::(chooser iT List.tl trueList)

我收到以下错误:

错误:此变体表达式应具有类型单元构造函数::不属于单元

我究竟做错了什么?

2 回答

  • 1

    没有 elseif ... then 的结果必须是 unit ,因为当表达式为false时,该值将为 () (类型 unit 的值) .

    换句话说,您需要一个 else 部件,以便 if 获得您想要的类型 . 当比较为假时,该值应该是多少?

  • 1

    else 部分没有明确定义 - 所以当条件不满足时,else部分是()(即单位) . 编译器typechecks iH::(chooser iT List.tl trueList)unit ,但不是这样的:

    if cond
        then A
        else B
    

    AB 具有相同的类型 .

相关问题