Pergunta

Possível duplicata:
F#] Como ter dois métodos se chamando?

Olá a todos,

Eu tenho um cenário em que tenho duas funções que se beneficiariam de serem mutuamente recursivas, mas não tenho muita certeza de como fazer isso em f#

Meu cenário não é tão simples quanto o código a seguir, mas eu gostaria de obter algo semelhante à compilação:

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
Foi útil?

Solução

Você também pode usar let rec ... and Formato:

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

Outras dicas

Para obter funções mutuamente recursivas, simplesmente passe uma para a outra como um parâmetro

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

Use o let rec ... and ... construir:

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
Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top