I am trying to define a recursive lambda expression in scala and struggling a bit with sintax, if think that this is just a syntax: Here is what I have so far (not compilable):
type rec = (Int => Int, Int)
val f = (x : Int) => x + x
val y : rec = (f:Int => Int, x:Int) => if (x > 0) y( f, 1) else 1
Error I am getting:
ScalaFiddle.scala:14: error: ScalaFiddle.rec does not take parameters
val y : rec = (f:Int => Int, x:Int) => if (x > 0) y( f, 1) else 1
^
Original code sample I am trying to optimize (which works fine):
case class Rec[I, O](fn : (I => O, I) => O) extends (I => O) {
def apply(v : I) = fn(this, v)
}
val sum = Rec[Int, Int]((f, v) => if (v == 0) 0 else v + f(v - 1))
Any help would be appreciated.
Thanks