syntax-error - 嵌套函数和变量的 ocaml 问题

标签 syntax-error 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

最佳答案

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

let name = expression

作为一种方便的语法,您可以在最外层定义一个函数,如下所示:
let name arg = expression
let 的另一种形式可以出现在任何地方,用于定义局部变量。它看起来像这样:
let name = expression1 in expression2

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

同样,作为一种方便的语法,您可以像这样定义一个本地函数:
let name arg = expression1 in expression2

在我看来它像 auxListMakerauxTrueFlser应该是定义在 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是在顶层定义的(所以只有 let ,没有 in )。其他名称是局部变量(值和函数),因此用 let ... in 定义。 .

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

更新

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

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

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

关于syntax-error - 嵌套函数和变量的 ocaml 问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50340708/

相关文章:

php - 更新时出现 MYSQL 语法错误 'Key' 字

oracle - plsql存储过程采用参数给出错误

marshalling - 哪些OCaml标准库类型无法编码?

ocaml - 如何将构造函数作为参数传递,然后对其进行模式匹配?

algorithm - Ocaml作业需要一些建议

python-3.x - VSCode上带有Pylint的打印功能获得无效的语法<未知,第6行>错误

ruby-on-rails - RubyMine语法错误-新版ruby 2.1.2

flutter - Flutter错误: The element type 'Button' can't be assigned to the list type 'Widget'

ocaml - OPAM 和 OCaml 安装

syntax-error - #load “unix.cma”导致语法错误