def fix[A, B](fn : Function2[Function1[A, B], A, B]) : Function1[A, B] =
(x : A) => fn(fix(fn), x)
lazy val fibs1 = fix[(Int, Int), Stream[Int]](
(fn, a) => a._1 #:: fn((a._2, a._1 + a._2))
)
val fibs2 = fix[(Int, Int), Stream[Int]](
(fn, a) => a._1 #:: fn((a._2, a._1 + a._2))
)
While learning Scala I ran into a strange error. Why is it that fibs1((1,1))
produces no error, while fibs2((1, 1))
gives a null pointer exception?
EDIT:
This code was inside a App class. It seems like val
s are not initialized until instance.main(...)
is evaluated. I assume the implementation for lazy val
s is different.