/ / Scala: (Int, Int) => Int non corrisponde (Int, Int) => Int - scala, divisore più comune, y-combinatore

Scala: (Int, Int) => Int non corrisponde (Int, Int) => Int - scala, massimo-comune-divisore, y-combinatore

Sto cercando di usare il combinatore y per definire gcd in scala:

object Main {
def y[A,B]( f : (A => B) => A => B ) : A => B = f(y(f))
def gcd = y[(Int,Int),Int]( (g) => (x,y) => if (x == 0) y else g(y % x, x) )
}

Ma sto ricevendo un errore:

Main.scala:3: error: type mismatch;
found   : (Int, Int) => Int
required: (Int, Int) => Int
def gcd = y[(Int,Int),Int]( (g) => (x :Int,y :Int) => if (x == 0) y else g(y % x, x) )
^

Se curry tutti gli argomenti, allora non c'è nessun problema:

def gcd = y[Int,Int => Int]( g => x => y => if (x == 0) y else g(y % x)(x) )

Cosa sto sbagliando nella versione senza fretta?

risposte:

8 per risposta № 1

Il po 'con (g) => (x :Int,y :Int) =>. Scala si aspetta che il tuo argomento sia una tupla di (Int, Int), quindi sarebbe più simile (g) => (tup: (Int, Int)) =>

È possibile utilizzare un po 'di corrispondenza dei motivi per evitare di doverli usare _1 e _2 corrispondenza su tup. Questo va bene per me:

def gcd = y[(Int, Int), Int](g => {
case (x,y) => if(x == 0) y else g(y % x, x)
})