OCaml 主要功能
OCaml main function
我需要一个主函数来 运行 其他函数。
我试过这个:
let main () =
let deck = make_mazo in
let jugadores = players [] 0 in
dothemagic deck jugadores 0 [] [] [];;
但是我得到了这个错误:
File "game.ml", line 329, characters 37-39:
Error: Syntax error
我认为 ;;
是问题所在,我需要一种不同的方式来结束代码。也尝试只使用 ;
,问题是一样的。
[编辑]
此处更新
let main =
let deck = make_mazo [] in
let game = players deck [] 0 in
let dd = fst game in
let jugadores = snd game in
dothemagic dd jugadores 0 [] [] [] [];
let () = main;;
错误仍然存在:
File "game.ml", line 253, characters 13-15:
Error: Syntax error
其他函数工作得很好,但我需要一个主要函数,因为我想 运行 使用 ocaml game.ml
或 ocamlbuild game.native
的程序
[第二次编辑]
@camlspotter 回复后:您的代码 ;
的使用是错误的。删除它。
更新 2.0
let main =
let deck = make_mazo [] in
let game = players deck [] 0 in
let dd = fst game in
let jugadores = snd game in
dothemagic dd jugadores 0 [] [] [] []
let () = main;;
新错误:
File "game.ml", line 253, characters 0-3: Error: Syntax error
认为 let
是现在的问题,所以我尝试使用这个
let main =
let deck = make_mazo [] in
let game = players deck [] 0 in
let dd = fst game in
let jugadores = snd game in
dothemagic dd jugadores 0 [] [] [] []
main;;
但错误是:
File "game.ml", line 253, characters 4-6:
Error: Syntax error
您在此处显示的代码在语法上没有任何错误。
很可能问题出在您未显示的部分末尾附近,例如文件的第 324 行附近。
如果非要我猜的话,我会说第 324 行以 in
结尾:-)
作为旁注,您还需要调用 这个主函数。您可能希望文件的最后一行是这样的:
let () = main ()
(这一行出现在我的许多 OCaml 项目中。)
在ocaml中,不像其他语言那样没有main函数,看下面的代码:
let () = print_string "hello\n";;
let f = print_string "hello, this is f\n";;
let () = f;;
OCaml 程序与许多其他语言的程序不同,它没有特定的入口点:模块(文件)中的所有代码都按从上到下的顺序求值,有点像脚本语言。您会看到的一个常见习语是:
let name = "World" (* I could have added a ;; at the
* end of this line, but that would
* have been unnecessary *)
let () =
Printf.printf "Hello, %s!\n" name
将输出
Hello, World!
let () = ...
可能看起来有点靠不住,但它实际上只是模式匹配:Printf.printf
的return类型是单位,()
也是类型unit
,你真的只是在说 "match this unit
value with the result of evaluating this expression"。基本上,这个成语的意思是 "run this unit-type expression in a safe way".
一个类似的,虽然非常不鼓励的习语,使用了包罗万象的模式:
let greet s =
match s with
| "" -> false
| _ ->
Printf.printf "Hello, %s!\n" s;
true
let name = "World"
let _ =
greet world
包罗万象的模式不关心它所匹配的表达式的类型(或值),这个习语的意思是 "run this expression and discard whatever it returned".
为了解决我的问题,我不得不按以下方式编写函数,
let fun x =
let y = blabla in
code_returning_unit; (* Use ; for unit returns *)
return_value;; (* Use ;; for end of fun *)
感谢大家的帮助。
我需要一个主函数来 运行 其他函数。
我试过这个:
let main () =
let deck = make_mazo in
let jugadores = players [] 0 in
dothemagic deck jugadores 0 [] [] [];;
但是我得到了这个错误:
File "game.ml", line 329, characters 37-39: Error: Syntax error
我认为 ;;
是问题所在,我需要一种不同的方式来结束代码。也尝试只使用 ;
,问题是一样的。
[编辑]
此处更新
let main =
let deck = make_mazo [] in
let game = players deck [] 0 in
let dd = fst game in
let jugadores = snd game in
dothemagic dd jugadores 0 [] [] [] [];
let () = main;;
错误仍然存在:
File "game.ml", line 253, characters 13-15: Error: Syntax error
其他函数工作得很好,但我需要一个主要函数,因为我想 运行 使用 ocaml game.ml
或 ocamlbuild game.native
[第二次编辑]
@camlspotter 回复后:您的代码 ;
的使用是错误的。删除它。
更新 2.0
let main =
let deck = make_mazo [] in
let game = players deck [] 0 in
let dd = fst game in
let jugadores = snd game in
dothemagic dd jugadores 0 [] [] [] []
let () = main;;
新错误:
File "game.ml", line 253, characters 0-3: Error: Syntax error
认为 let
是现在的问题,所以我尝试使用这个
let main =
let deck = make_mazo [] in
let game = players deck [] 0 in
let dd = fst game in
let jugadores = snd game in
dothemagic dd jugadores 0 [] [] [] []
main;;
但错误是:
File "game.ml", line 253, characters 4-6: Error: Syntax error
您在此处显示的代码在语法上没有任何错误。
很可能问题出在您未显示的部分末尾附近,例如文件的第 324 行附近。
如果非要我猜的话,我会说第 324 行以 in
结尾:-)
作为旁注,您还需要调用 这个主函数。您可能希望文件的最后一行是这样的:
let () = main ()
(这一行出现在我的许多 OCaml 项目中。)
在ocaml中,不像其他语言那样没有main函数,看下面的代码:
let () = print_string "hello\n";;
let f = print_string "hello, this is f\n";;
let () = f;;
OCaml 程序与许多其他语言的程序不同,它没有特定的入口点:模块(文件)中的所有代码都按从上到下的顺序求值,有点像脚本语言。您会看到的一个常见习语是:
let name = "World" (* I could have added a ;; at the
* end of this line, but that would
* have been unnecessary *)
let () =
Printf.printf "Hello, %s!\n" name
将输出
Hello, World!
let () = ...
可能看起来有点靠不住,但它实际上只是模式匹配:Printf.printf
的return类型是单位,()
也是类型unit
,你真的只是在说 "match this unit
value with the result of evaluating this expression"。基本上,这个成语的意思是 "run this unit-type expression in a safe way".
一个类似的,虽然非常不鼓励的习语,使用了包罗万象的模式:
let greet s =
match s with
| "" -> false
| _ ->
Printf.printf "Hello, %s!\n" s;
true
let name = "World"
let _ =
greet world
包罗万象的模式不关心它所匹配的表达式的类型(或值),这个习语的意思是 "run this expression and discard whatever it returned".
为了解决我的问题,我不得不按以下方式编写函数,
let fun x =
let y = blabla in
code_returning_unit; (* Use ; for unit returns *)
return_value;; (* Use ;; for end of fun *)
感谢大家的帮助。