5

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?

rampion
  • 82,104
  • 41
  • 185
  • 301

1 Answers1

9

The bit with (g) => (x :Int,y :Int) =>. Scala expects your argument to be a tuple of (Int,Int), so it would be more like (g) => (tup: (Int, Int)) =>

You can use a bit of pattern matching to avoid having to use _1 and _2 matching on tup. This compiles just fine for me:

def gcd = y[(Int, Int), Int](g => {
  case (x,y) => if(x == 0) y else g(y % x, x)
})
Dylan
  • 11,912
  • 2
  • 35
  • 64
  • 1
    Great! That gets it working. I'd also like to understand what the compiler was complaining about. Can you help interpret the error? When is a tuple not a tuple? – rampion Jan 21 '12 at 01:30
  • 1
    @rampion "When is a tuple not a tuple?" sounds like a great question in its own right. The error notation certainly is ambiguous. – Dan Burton Jan 21 '12 at 01:48
  • I agree that the error is ambiguous. Maybe I'm missing something, but it would be cool to be able to pass in tuples as argument lists, a la `val x = (1,"hi"); def foo(a:Int, b:String) = ...; foo(x)` – Dylan Jan 22 '12 at 04:02
  • @DanBurton: Apparently the answer is "when it's a set of arguments." Looks like the compiler displays "functions that take n arguments" and "functions that take a single n-tuple argument" identically, but treats them differently: https://gist.github.com/1655807 – rampion Jan 22 '12 at 05:57
  • 2
    in case anyone following this, `def y[A,B]( f : (A => B) => A => B ) : A => B = f(y(f))` should be written as `def y[A,B]( f : (A => B) => A => B ) : A => B = f(y(f))(_:A)` Otherwise, you will get `StackOverflowError` – thlim Feb 08 '19 at 17:25