Types Ocaml类型错误

Types Ocaml类型错误,types,int,ocaml,Types,Int,Ocaml,我正在学习OCaml,这是我的第一种打字语言,所以请对我耐心: 实际上,我试图定义一个函数“divides”,它输入两个整数,并输出一个布尔值,描述“inta”是否均匀地划分为“intb”。在我的第一次尝试中,我写了如下内容: let divides? a b = if a mod b = 0 then true else false;; let divides ?a b = if a mod b = 0 then true else false 这导致了类型错误: if a

我正在学习OCaml,这是我的第一种打字语言,所以请对我耐心:

实际上,我试图定义一个函数“divides”,它输入两个整数,并输出一个布尔值,描述“inta”是否均匀地划分为“intb”。在我的第一次尝试中,我写了如下内容:

let divides? a b =
if a mod b = 0 then true
else false;; 
let divides ?a b =
   if a mod b = 0 then true
   else false
这导致了类型错误:

if a mod b = 0 then true
  ^
Error: This expression has type 'a option
       but an expression was expected of type int
然后我试着扭转局面,我做到了:

let divides? a b =
 match a mod b with
  0 -> true
 |x -> false;;
这没什么帮助:
字符26-27
将a模式b与
^
错误:此表达式具有类型“a”选项
但表达式应为int类型

然后我试了一下:

let divides? (a : int) (b : int) =
 match a mod b with
 0 -> true
|x -> false;;
由此得出以下结论: 字符14-15: 让我们分开?(a:int)(b:int)= ^ 错误:此模式与int类型的值匹配 但需要一个与“a选项”类型的值匹配的模式


我现在对类型系统感到非常困惑和沮丧。(我的第一种语言是Scheme,这是我的第二种语言。)非常感谢您为我解释哪里出了问题以及如何解决问题提供建议。

问题是您不能使用问号字符?在OCaml中的变量/函数名中。它实际上像这样解析您的函数声明:

let divides? a b =
if a mod b = 0 then true
else false;; 
let divides ?a b =
   if a mod b = 0 then true
   else false
请注意,问号实际上影响的是
a
的类型,而不是函数名称的一部分

这意味着
a
是一个,因此对于某些
'a
,会为其分配类型
'a选项


请尝试从名称中删除问号。

(与大多数语言一样,您可以将
if-then true或false
替换为just
。请记住这一点。)非常感谢!!!我疯了,查课本,浏览互联网上的每一个网站,寻找答案。。。谢谢。没问题,很高兴我们能帮忙!