ocaml类型不匹配单元与列表

编程入门 行业动态 更新时间:2024-10-23 22:29:04
本文介绍了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

我做错了什么?

推荐答案

不包含else的if ... 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类型不匹配单元与列表

本文发布于:2023-11-26 00:10:52,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1631891.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:不匹配   单元   类型   列表   ocaml

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!