在SML中引发异常编译
我已经用SML编写了下面的代码,但是我遇到了编译错误在SML中引发异常编译,sml,Sml,我已经用SML编写了下面的代码,但是我遇到了编译错误 fun getTransIndex(t : TRANSACTION, (h::L) : TRANSLIST) : int = let val i=0 in if (h=t) then i else if (getTransIndex(t,L)<>~1 then getTransIndex(t,L)+1 else ~1 end getTr
fun getTransIndex(t : TRANSACTION, (h::L) : TRANSLIST) : int =
let val i=0
in if (h=t) then i
else
if (getTransIndex(t,L)<>~1
then getTransIndex(t,L)+1
else ~1
end
getTransIndex(_,[] )=~1;
fun-getTransIndex(t:TRANSACTION,(h::L):TRANSLIST):int=
设vali=0
在if(h=t)中,则i
其他的
if(getTransIndex(t,L)~1
然后getTransIndex(t,L)+1
else~1
结束
getTransIndex(_,[])=~1;
事务是值为1..3的索引
TRANSLIST是事务的列表
函数getTransIndex
获取的事务标识为
colset事务作为第一个参数和
使用colset TRANSLIST作为第二个参数的事务
并返回事务在列表中的位置索引
(从0开始计数)。如果列表不包含此事务,则
函数返回-1作为结果
所以我想解决这些问题。解决方案是什么?这有几个问题: 1) 行中有多余的不匹配的左括号
if (getTransIndex(t,L)<>~1 then
没有理由明确地告诉SML,
~1
是一个int。另一方面,\uu:transaction
不能按预期工作,因为\uu
不是一个普通的标识符,因此,我将类型注释:transaction
移动到包含t
的行中,实际的错误消息是什么?非常彻底的反馈。:)我认为行getTransIndex(,[])=~1
是空列表案例的定义,但是缺少了它的|
@molbdnilo您无疑是正确的。我习惯于在定义中首先看到空的列表基本情况,因此我将该行作为其上方函数定义的测试用例来阅读。我将编辑答案。谢谢
if getTransIndex(t,L) <> ~1 then
type transaction = int
type translist = int list
fun getTransIndex(_,[]:translist) = ~1
| getTransIndex(t:transaction,(h::L)) =
if (h=t) then
0
else
if getTransIndex(t,L) <> ~1 then
getTransIndex(t,L)+1
else ~1;