Can you explain me why the first definition is wrong compared to the next?
Why writing
((r._2,c))._1
will fetch me penultimate element?Please give me a trace how elements are inserted in
(r,c)
or their significance.
Here is the code:
scala> def penpen [A] (list:List[A]):A = {
list.foldLeft( (list.head, list.tail.head) )((r, c) => (r,c))._1
}
8: error: type mismatch;
found : r.type (with underlying type (A, A))
required: A
list.foldLeft( (list.head, list.tail.head) )((r, c) => (r,c))._1
^
scala> def penpen [A] (list:List[A]):A = {
list.foldLeft( (list.head, list.tail.head) )((r, c) =>(r._2,c))._1
}
penpen: [A](list: List[A])A