在 F# 中编写相互递归函数时出现问题
我正在翻译 Little Mler 的一个函数,该函数对该数据类型进行操作
type sexp<'T> =
An_Atom of 'T
| A_slist of slist<'T>
and
slist<'T> =
Empty
| Scons of sexp<'T> * slist<'T>
该函数
// occurs_in_slist : aVal slist -> int
// checks the number of occurrence for aVal in slist
let rec occurs_in_slist =
function
_, Empty-> 0
| (aVal : 'T), Scons(aSexp, (aSlist : 'T)) ->
occurs_in_sexp (aVal, aSexp) + occurs_in_slist (aVal, aSlist)
and
aVal, An_Atom (bVal) -> if (aVal = bVal) then 1 else 0
| (aVal , A_slist(aSlist)) -> occurs_in_slist (aval, aSlist)
但是,我在第二个函数中收到此错误
error FS0010: Unexpected symbol '->' in binding. Expected '=' or other token.
I am translating a function from Little Mler that operates on this data type
type sexp<'T> =
An_Atom of 'T
| A_slist of slist<'T>
and
slist<'T> =
Empty
| Scons of sexp<'T> * slist<'T>
The function
// occurs_in_slist : aVal slist -> int
// checks the number of occurrence for aVal in slist
let rec occurs_in_slist =
function
_, Empty-> 0
| (aVal : 'T), Scons(aSexp, (aSlist : 'T)) ->
occurs_in_sexp (aVal, aSexp) + occurs_in_slist (aVal, aSlist)
and
aVal, An_Atom (bVal) -> if (aVal = bVal) then 1 else 0
| (aVal , A_slist(aSlist)) -> occurs_in_slist (aval, aSlist)
However, I get this error for the second function
error FS0010: Unexpected symbol '->' in binding. Expected '=' or other token.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
在函数定义中,您使用了
and
关键字来定义一组相互递归的函数,但是您只为第一个函数指定了名称。它需要and
之后的另一个函数的名称,这就是您收到错误的原因。不幸的是你忽略了这一点。我相信这就是您想要做的:
尽管我觉得这里更合适的返回类型应该是
bool
。In your function definition, you've used the
and
keyword to define a mutually recursive set of functions however you've only given a name for the first function. It's expecting the name of the other function after theand
which is why you're getting the error. Unfortunately you've left that out.I believe this is what you were trying to do:
Though I feel the more appropriate return type here should be a
bool
.