ocaml类型不匹配单元与列表 [英] ocaml type missmatch unit vs list

查看:66
本文介绍了ocaml类型不匹配单元与列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

为此签名

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)

我收到以下错误:

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

Error: This variant expression is expected to have type unit The constructor :: does not belong to unit

我做错了什么?

推荐答案

不包含elseif ... then的结果必须为unit,因为该值将为()(类型为)表达式为假时.

The result of if ... then with no else has to be unit, because the value will be () (the value of type unit) when the expression is false.

换句话说,您需要为if使用else部分来获取所需的类型.比较为假时,该值应该是什么?

In other words, you need an else part for your if to get the type you want. What should the value be when the comparison is false?

这篇关于ocaml类型不匹配单元与列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆