Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

What does the following Scala expression return? List(1,2,2,3,3,3,4,4,4,4).foldR

ID: 3596214 • Letter: W

Question

What does the following Scala expression return?

List(1,2,2,3,3,3,4,4,4,4).foldRight[(List[Int], List[List[Int]])]((Nil, Nil)) {

(h, acc) => acc match {

case (Nil, pacc) => (h :: Nil, pacc)

case (lacc @ (p :: _), pacc) =>

if (h == p) (h :: lacc, pacc) else (h :: Nil, lacc :: pacc)

}

}

What does the following Scala expression return? List (1,2,2,3,3,3,4,4,4,4).foldRight [(List [Int], List[List [Int (h, acc) => acc match { case (Nii, pacc ) => (h :: Nii, pacc ) case (lacc @ (p :.-), pa c c ) => Select one: a. (1,List(2,2), List(3,3,3), List(4,4,4,4) b. (List(1),List(List(2,2), List (3,3,3),List(4,4,4,4) O c. List(1,2,3,4) d. List(List(1),List(2,2),List(3,3,3),List(4,4,4,4) e. List(List(2,2),List(3,3,3),List(4,4,4,4) O f·(1,List(List(2.2),List(3,3,3),List(4,4,4,4))) O g. List (1,2,2,3,3,3,4,4,4,4) Check

Explanation / Answer

Ans. f :  (List(1),List(List(2, 2), List(3, 3, 3), List(4, 4, 4, 4)))

The foldRight method takes an associative binary operator function as parameter and will use it to collapse elements from the collection. The order for traversing the elements in the collection is from right to left