F#:相互递归函数 [英] F#: Mutually recursive functions

查看:156
本文介绍了F#:相互递归函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

大家好,

我有一个场景,我有两个函数可以从相互递归中受益,但我不确定如何在F#中做到这一点

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

推荐答案

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

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

这篇关于F#:相互递归函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆