Pregunta

Posible duplicado:
F#] ¿Cómo tener dos métodos llamarse entre sí?

Hola a todos,

Tengo un escenario en el que tengo dos funciones que se beneficiarían de ser mutuamente recursivo, pero no estoy realmente seguro de cómo hacer esto en f#

Mi escenario no es tan simple como el siguiente código, pero me gustaría obtener algo similar a la compilación:

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
¿Fue útil?

Solución

También puedes usar let rec ... and forma:

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

Otros consejos

Para obtener funciones recursivas mutuamente, simplemente pase una a la otra como 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

Utilizar el 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 bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top