首页 > 解决方案 > 嵌套函数和变量的 ocaml 问题

问题描述

我正在尝试根据具有值 [value] 的列来限制表的值的函数 retric。我的想法是为满足条件列和值参数的列制作一个由真假组成的列表。稍后递归匹配将选择列,listmaker 函数将根据真假列表创建一个新列。

当谈到存储在 ocaml 嵌套变量中时,让...在范围界定中我很困惑。下面的代码有什么问题?

let rec restrict (column, value, aTable) = match aTable with
    name,[]->[]
  |name,(col,vals)::rest->if col=column
      then (col,auxListMaker(vals,trueFalseList))::restrict (column,value.(name,rest))
      else restrict (column,value.(name,rest))

let rec auxTrueFalser (column, value, aTable) = match aTable with 
    name,[]->[]
  |name,(col,vals)::rest-> if column=col 
      then (if List.hd vals = value 
            then true::aux1(column,value,(name,[(col,List.tl vals)]))
            else false::aux1(column,value,(name,[(col,List.tl vals)])))
      else aux1(column,value,(name,rest)) 
in 

let trueFalseList =  auxTrueFalser (column, value, aTable) in

let rec auxListMaker (vals, trueFalseList) = match vals with
    []->[]
  |h::t -> if List.hd trueFalseList
      then h::auxListMaker(t,List.tl trueFalseList)
      else auxListMaker(t,List.tl trueFalseList)
in

标签: syntax-errorocaml

解决方案


要意识到的主要事情是let. 第一种形式用于定义模块中的值,并且必须出现在模块的最外层。它看起来像这样:

let name = expression

作为一种方便的语法,您可以在最外层定义一个函数,如下所示:

let name arg = expression

另一种形式let可以出现在任何地方,用于定义局部变量。它看起来像这样:

let name = expression1 in expression2

这建立name为具有由 给出的值的局部变量expression1。名称的范围(可以使用的地方)是expression2.

同样,作为一种方便的语法,您可以像这样定义一个本地函数:

let name arg = expression1 in expression2

在我看来,它auxListMaker应该auxTrueFlser是内部定义的局部函数restrict。并且trueFalseList应该是本地(非功能)值。所以形式restrict会是这样的:

let rec restrict (column, value, aTable) =

    let auxTrueFalser (column, value, aTable) =
        ...
    in

    let auListMaker (vals, trueFalseList) =
        ...
    in

    let trueFalseList = auxTrueFalser (column, value, aTable) in

    ... (* body of restrict *)

在这个布局restrict中定义在顶层(所以只是 with let, no in​​)。其他名称是局部变量(值和函数),因此用let ... in.

另请注意,您必须在使用之前定义名称。在您的代码中,名称auxListMaker在定义之前使用。在上面的布局中,顺序是可以的。

更新

对进一步问题的回答。

第一种形式let(没有in)的范围是模块的其余部分。对于简单的 .ml 源文件的常见情况,这意味着文件的其余部分。

是的,上述示意图布局中显示的函数将在每次递归调用restrict时重新评估该值。trueFaleList

如果您在 OCaml 知道的终端上工作,它会强调它认为您有语法错误的地方。上面原理图布局的前几行的语法显然是可以的。您必须显示您的代码(或显示问题的最小独立子集)和您收到的特定错误消息。


推荐阅读