Question

I'm trying to use the y-combinator to define 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) )
}

But I'm getting an error:

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) ) 
                                                       ^

If I curry all the arguments, then there's no problem:

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

What am I doing wrong in the uncurried version?

No correct solution

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top