Hi all I am fairly new to Scala coming from C#.
I am attempting to write my own version of accumulate ( fold) I am wondering why I am running into some issues with the following:
def accumulate[T](list : List[T], initial: T, f: (T, T) => T) : T = {
@tailrec def loop[T](list: List[T], accum: T) : T =
if(list.length == 0)
accum
else{
val head : T = list.head
val res : T = f(accum,head)
loop[T](list.tail, res)
}
loop(list,initial)
}
I am getting the following error:
type mismatch;
found : accum.type (with underlying type T)
required: T
val res : T = f(accum,head)
^
I cant see how I have a type mismatch considering everything is type T.
Any thoughts / help would be appreciated.
Blair