Syntax OCaml语法环境和语法错误
输出为150 我的问题是:为什么“print_int”还没有执行?这是因为Syntax OCaml语法环境和语法错误,syntax,syntax-error,ocaml,Syntax,Syntax Error,Ocaml,输出为150 我的问题是:为什么“print_int”还没有执行?这是因为funx->print\u int x只是定义了一个函数,但还不需要执行吗?内部函数只是打印15吗 我想回应我的猜测,当我修改代码时: let x = 132;; let f x = let x = 15 in (fun x -> print_int x) 150;; f 2;; 系统将提示一个错误。为什么? (我只是想把函数命名为“g”,但语法错误?) 有人能帮忙吗?thx要解决语法错误,您必须这样编写
funx->print\u int x
只是定义了一个函数,但还不需要执行吗?内部函数只是打印15吗
我想回应我的猜测,当我修改代码时:
let x = 132;;
let f x =
let x = 15 in (fun x -> print_int x) 150;;
f 2;;
系统将提示一个错误。为什么?
(我只是想把函数命名为“g”,但语法错误?)
有人能帮忙吗?thx要解决语法错误,您必须这样编写(您在关键字和函数名中缺少
):
要了解为什么要在顶层查看第一个示例的类型:
let f x =
let x = 15 in let g x = print_int x in g 150;;
#(乐趣x->print_int x);;(*定义函数*)
-:int->unit=
#(乐趣x->print_int x)150;;(*定义一个函数并用150*调用它)
150-:单位=()
#(设gx=print_int x);;(*定义一个名为“g”的值,该值是一个函数,“g”的类型如下*)
val g:int->unit=
#(设gx=print_int x)150;;(*您不能这样做,paranthesis中的代码不是值:它是let绑定或值的定义*)
错误:语法错误
f x
中的x
和让x=15
与函数中的x无关,最里面的x
优先(这称为阴影)
let f x =
let x = 15 in let g x = print_int x in g 150;;
# (fun x -> print_int x);; (* define a function *)
- : int -> unit = <fun>
# (fun x -> print_int x) 150;; (* define a function and call it with 150 *)
150- : unit = ()
# (let g x = print_int x);; (* define a value named 'g' that is a function , 'g' has the type below *)
val g : int -> unit = <fun>
# (let g x = print_int x) 150;; (* you can't do this, the code in the paranthesis is not a value: it is a let-binding or a definition of a value *)
Error: Syntax error