ocaml int 和 unsigned int
ocaml int and unsigned int
我试图在 ocaml 的数组中找到某个整数的最后一次出现:
let rec findLastHelper l i loc ret =
match l with
| [] -> ret
| x::xs ->
match x == i with
| true -> findLastHelper xs i (loc+1) loc
| _ -> findLastHelper xs i (loc+1) ret ;;
let findLast l i = (findLastHelper l i 0 -1) ;;
(* let findLast l i = (findLastHelper l i 0 493) ;; *)
let main = Printf.printf "%d\n" (findLast [ 1 ; 6 ; 8 ; 2 ; 9 ; 8 ] 7) ;;
如果整数不存在,代码应该return -1
。当我编译它时,出现以下错误:
$ ocamlopt main.ml -o main
File "main.ml", line 9, characters 20-40:
Error: This expression has type int -> int
but an expression was expected of type int
当我将 -1
替换为任意 正值 (上面的 493)时,一切正常。
这是怎么回事?
OCaml 将 -
解释为此上下文中的二元运算符。你必须用括号 (-1)
.
这是一个常见的 OCaml 问题。
我试图在 ocaml 的数组中找到某个整数的最后一次出现:
let rec findLastHelper l i loc ret =
match l with
| [] -> ret
| x::xs ->
match x == i with
| true -> findLastHelper xs i (loc+1) loc
| _ -> findLastHelper xs i (loc+1) ret ;;
let findLast l i = (findLastHelper l i 0 -1) ;;
(* let findLast l i = (findLastHelper l i 0 493) ;; *)
let main = Printf.printf "%d\n" (findLast [ 1 ; 6 ; 8 ; 2 ; 9 ; 8 ] 7) ;;
如果整数不存在,代码应该return -1
。当我编译它时,出现以下错误:
$ ocamlopt main.ml -o main
File "main.ml", line 9, characters 20-40:
Error: This expression has type int -> int
but an expression was expected of type int
当我将 -1
替换为任意 正值 (上面的 493)时,一切正常。
这是怎么回事?
OCaml 将 -
解释为此上下文中的二元运算符。你必须用括号 (-1)
.
这是一个常见的 OCaml 问题。