I am a scala noob, learning through functional programming in scala book. i was doing some exercises, and this thing really startled me:
def plusone[Int](l: List[Int]): List[Int] = foldRight(l, Nil:List[Int])((x,xs)=>Cons(x+1,xs))
This is a function that would create a new list with ones added to each elements, but when I wrote this, I got this error:
type mismatch; found : Int(1) required: String List.scala /exercises/src/main/scala/fpinscala/datastructures line 94 Scala Problem
Funny thing is, when I removed that [Int]from my function declaration(so just def plusone, and all others stayed same) I didn't have any error. I am at a loss how to understand this, I would appreciate any help you guys can give me.
FYI, some functions I am using there: sealed trait List[+A]
case object Nil extends List[Nothing]
case class Cons[+A](head: A, tail: List[A]) extends List[A]
def foldRight[A,B](as: List[A], z: B)(f: (A, B) => B): B =
as match {
case Nil => z
case Cons(x, xs) => f(x, foldRight(xs, z)(f))
}
All others should be generic, I believe.