F#:相互递归函数

发布于 2024-09-17 06:26:21 字数 543 浏览 6 评论 0原文

可能的重复:
[F#] 如何让两个方法相互调用?

大家好,

我有一个场景,我有两个函数可以从相互递归中受益,但我不太确定如何在 F# 中执行此操作

我的场景并不像下面的代码那么简单,但我想得到类似于编译的东西:

let rec f x =
  if x>0 then
    g (x-1)
  else
    x

let rec g x =
  if x>0 then
    f (x-1)
  else
    x

Possible Duplicate:
[F#] How to have two methods calling each other?

Hello all,

I Have a scenario where I have two functions that would benefit from being mutually recursive but I'm not really sure how to do this in F#

My scenario is not as simple as the following code, but I'd like to get something similar to compile:

let rec f x =
  if x>0 then
    g (x-1)
  else
    x

let rec g x =
  if x>0 then
    f (x-1)
  else
    x

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(3

女皇必胜 2024-09-24 06:26:21

您还可以使用 let rec ... and 形式:

let rec f x =
  if x>0 then
    g (x-1)
  else
    x

and g x =
  if x>0 then
    f (x-1)
  else
    x

You can also use let rec ... and form:

let rec f x =
  if x>0 then
    g (x-1)
  else
    x

and g x =
  if x>0 then
    f (x-1)
  else
    x
神爱温柔 2024-09-24 06:26:21

要获得相互递归函数,只需将一个函数作为参数传递给另一个函数

let rec f g x =
  if x>0 then
    g (x-1)
  else
    x

let rec g x =
  if x>0 then
    f g (x-1)
  else
    x

To get mutually recursive functions simply pass one to the other as a parameter

let rec f g x =
  if x>0 then
    g (x-1)
  else
    x

let rec g x =
  if x>0 then
    f g (x-1)
  else
    x
要走就滚别墨迹 2024-09-24 06:26:21

使用 let rec ... and ... 构造:

let rec f x =
  if x>0 then
    g (x-1)
  else
    x

and g x =
  if x>0 then
    f (x-1)
  else
    x

Use the let rec ... and ... construct:

let rec f x =
  if x>0 then
    g (x-1)
  else
    x

and g x =
  if x>0 then
    f (x-1)
  else
    x
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文