在 F# 中编写相互递归函数时出现问题

发布于 2024-12-05 08:12:40 字数 808 浏览 4 评论 0原文

我正在翻译 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

一百个冬季 2024-12-12 08:12:40

在函数定义中,您使用了 and 关键字来定义一组相互递归的函数,但是您只为第一个函数指定了名称。它需要 and 之后的另一个函数的名称,这就是您收到错误的原因。不幸的是你忽略了这一点。

我相信这就是您想要做的:

let rec occurs_in_slist = function
  | _        , Empty -> 0
  | aVal : 'T, Scons(aSexp, aSlist : slist<'T>) -> 
        occurs_in_sexp (aVal, aSexp) + occurs_in_slist (aVal, aSlist)
and occurs_in_sexp = function
  | aVal : 'T, An_Atom(bVal) -> if (aVal = bVal) then 1 else 0
  | aVal     , A_slist(aSlist) -> occurs_in_slist (aVal, aSlist)

尽管我觉得这里更合适的返回类型应该是 bool

let rec occurs_in_slist = function
  | _        , Empty -> false
  | aVal : 'T, Scons(aSexp, aSlist : slist<'T>) -> 
        occurs_in_sexp (aVal, aSexp) || occurs_in_slist (aVal, aSlist)
and occurs_in_sexp = function
  | aVal : 'T, An_Atom(bVal) -> aVal = bVal
  | aVal     , A_slist(aSlist) -> occurs_in_slist (aVal, aSlist)

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 the and which is why you're getting the error. Unfortunately you've left that out.

I believe this is what you were trying to do:

let rec occurs_in_slist = function
  | _        , Empty -> 0
  | aVal : 'T, Scons(aSexp, aSlist : slist<'T>) -> 
        occurs_in_sexp (aVal, aSexp) + occurs_in_slist (aVal, aSlist)
and occurs_in_sexp = function
  | aVal : 'T, An_Atom(bVal) -> if (aVal = bVal) then 1 else 0
  | aVal     , A_slist(aSlist) -> occurs_in_slist (aVal, aSlist)

Though I feel the more appropriate return type here should be a bool.

let rec occurs_in_slist = function
  | _        , Empty -> false
  | aVal : 'T, Scons(aSexp, aSlist : slist<'T>) -> 
        occurs_in_sexp (aVal, aSexp) || occurs_in_slist (aVal, aSlist)
and occurs_in_sexp = function
  | aVal : 'T, An_Atom(bVal) -> aVal = bVal
  | aVal     , A_slist(aSlist) -> occurs_in_slist (aVal, aSlist)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文