F#: funzioni reciprocamente ricorsive [duplicate
-
26-09-2019 - |
Domanda
Possibile duplicato:
F#] Come avere due metodi che si chiamano?
Ciao a tutti,
Ho uno scenario in cui ho due funzioni che trarrebbero beneficio dall'essere reciprocamente ricorsivo, ma non sono davvero sicuro di come farlo in F#
Il mio scenario non è semplice come il seguente codice, ma mi piacerebbe ottenere qualcosa di simile a compilare:
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
Soluzione
Puoi anche usare let
rec
... and
modulo:
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
Altri suggerimenti
Per ottenere funzioni reciprocamente ricorsive, basta passare l'una all'altra come parametro
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
Utilizzare il let rec ... and ...
costruire:
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
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow