【问题标题】:Common Parse error: "in" in a function that call other functions OCaml常见解析错误:调用其他函数 OCaml 的函数中的“in”
【发布时间】:2016-08-15 02:13:25
【问题描述】:

这个Parse error: "in" expected after [binding] (in [expr]) 是我在Ocaml 用户中搜索到的常见错误,但是在我看到的示例中我没有找到错误的答案,那么我将解释我的问题:

我声明了这个函数:

let rec unit_propag xs  =

    let cuAux = teste xs 
    let updatelist = funfilter (List.hd(List.hd cuAux)) (xs) 
    let updatelist2 = filtraelem (negar(List.hd(List.hd cuAux))) (updatelist)

if(not(List.mem [] xs) && (teste xs <> []))
then 
    unit_propag updatelist2
;;

我在这段代码中使用的函数之前是这样声明的:

 let funfilter elem xs = List.filter (fun inner -> not (List.mem elem inner)) xs;;

let filtraele elem l = List.map( fun y -> List.filter (fun x -> x <> elem) y)l;;

let teste xs = List.filter(fun inner ->(is_single inner)inner)xs;;

let is_single xs = function
|[_]    -> true
|_  -> false
;;
let negar l =
match l with
V x -> N x
|N x    -> V x
|B  -> T
|T  -> B
;;  

但不是按照这个顺序。 好吧,他们都在做我想做的事,但是现在当我声明 unit_propag 并尝试编译时,我在

行中遇到了错误
let cuAux = teste xs

它说:

File "exc.ml", line 251, characters 20-22:
Parse error: "in" expected after [binding] (in [expr])
Error while running external preprocessor
Command line: camlp4o 'exc.ml' > /tmp/ocamlpp5a7c3d

然后我尝试在每个函数的末尾添加;,然后我的"in"错误出现在最后一个函数的行,是本案unit_propag updatelist2

我做错了什么?人们通常说这种错误发生在那个代码之前,但是当我评论这个函数时,程序编译完美。

我需要发布更多我的代码吗?或者我需要更清楚地回答我的问题? 在Ocaml 中是否可以做到这一点,或者我正在做一些我做不到的事情?

谢谢

【问题讨论】:

  • 使用适当的自动缩进工具,如 ocaml-mode、tuareg-mode 或 ocp-indent 可以轻松避免此类问题。如果自动缩进结果与您认为的不同,则很可能是您在附近的某个地方犯了语法错误。
  • 我现在正在使用自动缩进程序 thnx 作为提示

标签: list function multidimensional-array ocaml


【解决方案1】:

错误消息说你缺少in,所以通过添加; 来解决它似乎很奇怪:-)

无论如何,在函数unit_propag 中的所有let 关键字之后,您都缺少关键字in

你应该这样写:

let rec unit_propag xs  =
    let cuAux = teste xs in
    let updatelist = funfilter (List.hd(List.hd cuAux)) (xs) in
    let updatelist2 =
        filtraelem (negar(List.hd(List.hd cuAux))) (updatelist)
    in
    if (not (List.mem [] xs) && (teste xs <> [])) then 
       unit_propag updatelist2

这里已经多次解释了基本问题(如您所见)。关键字let 基本上有两种用途。在外层,它定义了模块中的值。在另一个定义中,它定义了一个局部变量,必须后跟in。这三个lets 在unit_propag 的定义中。

另一个解释let 用法的尝试在这里:OCaml: Call function within another function

【讨论】:

  • 不敢相信我没有测试过!谢谢,现在我更了解这种情况了
猜你喜欢
  • 1970-01-01
  • 2017-01-15
  • 2012-11-04
  • 2012-11-15
  • 2018-06-09
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多