syntax-error - 在 ocaml 中出现语法错误
问题描述
我正在尝试制作一个允许在 ocaml 中创建表的模块。它会执行一个查询project
来限制表的值。然而,在函数选择器定义的最后一行,我得到了语法错误。
module type TABLE =
sig
type database
type table
val create_table: string list * string list* (string list) list -> table
val printTable : table -> string
val listToString : string list -> string
val project : string list * table -> table
val chooser : string list * string list-> string list
end;;
module UsingTable : TABLE =
struct
type table = (string list * string list* (string list) list)
type database = table list
let create_table (a,b,c) = (a,b,c)
let chooser inputList = (
for i = 0 to (List.length trueFalseList-1) do
if List.nth trueFalseList i = "True"
then
(List.nth inputList i)::ans
done
List.rev ans;;)
let project (conditions, aTable)= (
let rec innerProc tmp= function
n,[],v->List.rev tmp
|n,cH::cT,v-> if List.mem cH conditions
then innerProc (["True"]::tmp) (n,cT,v)
else innerProc (["False"]::tmp) (n,cT,v)
in
let trueFalseList = innerProc [] aTable
let rec finalListCreator = match aTable with
n,[],[]->n,[],[]
|n,cH::cT,[]->n,chooser cH ::finalListCreator cT,[]
|n,c,h::t -> n,c,chooser h ::finalListCreator t
)
let rec listToString aList = match aList with
[] -> ""
| h::t -> "\t"^h^"\t"^listToString t
let rec printTable aTable = match aTable with
[],[],[] -> ""
| [],[],vH::vT -> "\n"^(listToString vH)^printTable ([],[],vT)
| [],cH::cT,v -> "\t"^cH^"\t"^printTable([],cT, v)
| n, c , v-> "\n"^(List.hd n)^"\n\n"^printTable([],c, v)
end;;
let atable =UsingTable.create_table (["Student"], ["Id";"Name";"Gender";"Course"],
[["001";"Jim";"M";"AlgoDS"];
["002";"Linnea";"F";"Databases"];
["003";"Anna";"F";"C#"];
["004";"Abby";"F";"C#"];
["005";"Arthur";"M";"JavaScript"]]);;
print_string (UsingTable.printTable atable) ;;
解决方案
这些行至少有两个语法问题:
let chooser inputList = (
for i = 0 to (List.length trueFalseList-1) do
if List.nth trueFalseList i = "True"
then
(List.nth inputList i)::ans
done
List.rev ans;;)
首先,for .. done
是一个表达式,并且List.rev ans
是另一个表达式。您需要;
在它们之间使用分号 ( )。
其次,您应该;;
仅在您希望处理该点之前的输入时使用。但是在这里,如果您在 处处理输入,;;
则缺少右括号。
在我看来,你应该只进入;;
顶层。考虑这个令牌的最佳方式是作为对顶层的指令。它不是正常 OCaml 语法的一部分。
这些只是前两个错误。代码中还有很多其他错误。一次向模块添加一个功能可能会很好,这样您就可以一次专注于几个问题。
更新
您正在使用的环境有点复杂,因为它有一个评估按钮,要求评估您迄今为止输入的内容。这使得;;
令牌的用处大大降低。
;;
在完全不使用令牌的情况下使用这个环境将是一个很好的纪律。当您需要评估时,只需单击评估按钮。
主要技巧是,如果您想在外部级别评估语句(OCaml 中的单位值表达式),例如 say Printf.printf "hello world\n"
。避免;;
在此之前放置的常用习惯用法是将其放入如下声明中:
let () = Printf.printf "hello world\n"
这是人们在编写源代码时使用的一个不明显的习语(;;
在我的经验中几乎从未出现过)。
推荐阅读
- polymorphism - 指定 Julia 函数只能采用其内容为特定类型的字典/数组的正确方法是什么?
- ios - SwiftUI/Combine no updates to @ObservedObject from @Published
- oracle - How to retain double quotes in a column while loading a file using SQL Loader
- android - How to add two or more kotlin native modules on an iOS project
- javascript - How to move fixed div by scroll amount?
- vb.net - Is there a way or event to retrieve command line arguments on runtime
- android - FirebaseAuthUserCollisionException vs FirebaseAuthInvalidUserException when user account is disabled?
- scala - 使用scala将DataFrame单行转换为Spark中的列
- c++ - Qt5.14 找不到“windows.h”头文件?
- html - 我无法让这个背景图像覆盖桌面视图中的整个高度