【发布时间】:2021-05-20 21:04:01
【问题描述】:
type table = string * string list * string list list;;
let studentTable = ("Student",
["ID";"Name";"Gender";"Course"],
[["2";"Jim";"Male";"Geography"];
["4";"Linnea";"Female";"Economics"];
["7";"Steve";"Male";"Informatics"];
["9";"Hannah";"Female";"Geography"]]);;
let rec restrictMatch(attribute,value,attributeList,dataList) =
match (attribute,value,attributeList,dataList) with
([],_,_,_) -> false
| (_,[],_,_) -> false
| (_,_,[],_) -> false
| (_,_,_,[]) -> false
| ((wh::wt),value,(ah::at),(h::t)) ->
if wh=ah && value=h
then true
else restrictMatch(attribute,value,at,t)
let rec restrictRows(attribute,value,attributeList,dataList) =
match (attributeList,dataList) with
(_,[]) -> []
| ([],_) -> []
| ((ah::at),(h::t)) ->
if restrictMatch(attribute,value,attributeList,h)
then h::restrictRows(attribute,value,attributeList,t)
else restrictRows(attribute,value,attributeList,t)
let restriction (attribute,value,table) =
match table with
(name,attributeList,dataList)->
(attributeList,restrictRows(attribute,value,attributeList,dataList))
我正在尝试使用 OCaml 实现“限制”的关系代数函数。但是,当我尝试这样运行“限制”功能时:
restriction ("Gender", "Female", studentTable)
Ocaml 向我抛出异常:
第 1 行,字符 13-21: 错误:此表达式具有字符串类型,但表达式应为类型 '一个列表
但是,这对我来说没有意义,因为我 99% 确定我没有在代码中的任何地方传递列表。
【问题讨论】:
-
尝试添加一些类型注释。这将大大缩小可能的原因。
-
在
restrictionRows中,您将attribute与[]匹配,因此它是ocaml 的列表。在restriction中,您将"Gender"传递为attribute,然后在需要列表的restrictionRows中使用。
标签: ocaml