I'm a beginner Scala developer having problems with a coding exercise (5.12) taken by the book "Functional Programming in Scala" by Paul Chiusano.
I have this function here, called unfold, that takes an initial state and a function for producing a stream with the next states:
def unfold[A,S](z: S)(f: S => Option[(A,S)]): Stream[A] = f(z) match {
case Some((h,t)) => h #:: unfold(t)(f)
case _ => Stream.empty
}
For example, with this function one can create an infinite stream of objects, e.g.
def constant[A](a: A): Stream[A] = unfold(a)(_ => Some(a,a))
Now, I want to create the Fibonacci sequence, then I type:
def fibs: Stream[Int] = unfold((0,1))((a,b) => Some(a,(b,a+b)))
I get these errors:
Missing parameter type b
Expression of type Some[((Int,Int),(Nothing,String))] doesn't conform to expected type Option[(A_,(Int,Int))]
If I use the case keyword inside the anonymous function passed to unfold, like:
{ case (a,b) => Some(a,(b,a+b))}
everything's fine.
So my question is: what's the difference between the two implementations? Is it something about type inference that I don't understand?