我学习OCaml的,这是我的第一个类型的语言,所以请尽量宽容我:“鸿沟”Ocaml程序编写错误类型
对于实践中,我试图定义一个函数它输入两个int并输出一个布尔值,描述'int a'是否均匀分配到'int b'中。在我第一次尝试,我写了这样的事情:
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;;
哪些没有多大帮助: Characters 26-27 match a mod b with ^ Error: This expression has type 'a option but an expression was expected of type 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,这是我的第二语言。)任何帮助解释我要去哪里错误和建议如何解决它非常感谢。
(正如在大多数语言中,你可以用''代替'if then true else false'。请注意。) –
2012-07-05 23:06:39