Question
Scala Expressions What type (if any) does the following Scala expression have? List (None, Some (List (Some (1), None, Some (2)))) Question 1: List[Option[Option[List[Int]]]] List[List[Option[Option[Int]]]]
Scala Expressions
What type (if any) does the following Scala expression have?
List (None, Some (List (Some (1), None, Some (2))))
Question 1: List[Option[Option[List[Int]]]] List[List[Option[Option[Int]]]] This expression does not typecheck, so is rejected by the compiler. List[Option[List[Option[Int]]]] Option[List[Option[List[Int]]]]
2. Consider the following Scala function definitions:
def f [X,Y] (a:Option[X], g:X=>Y) : Option[Y] = { a match { case None => None case Some (x) => Some (g (x)) } }
def index [X] (xs:List[X], n:Int) : Option[X] = { xs match { case Nil => None case y::ys if (n == 0) => Some (y) case _::ys => index (ys, n - 1) } }
What is the output of running:
f (index (List (10, 11, 12), 1), (n:Int) => n * 2)
Question : This expression does not typecheck, so is rejected by the compiler. Some(20):Option[Int] List(20,22,24):List[Int] Some(22):Option[Int] None:Option[Int]
3.
Consider the following Scala function definitions:
def f [X,Y] (a:Option[X], g:X=>Y) : Option[Y] = { a match { case None => None case Some (x) => Some (g (x)) } }
def index [X] (xs:List[X], n:Int) : Option[X] = { xs match { case Nil => None case y::ys if (n == 0) => Some (y) case _::ys => index (ys, n - 1) } }
What is the output of running:
f (index (List (10, 11, 12), 5), (n:Int) => n * 2)
Question : This expression fails at runtime because the index 5 is out of bounds for the list. Nil:List[Int] None:Option[Int] List(20,22,24):List[Int]
4. Consider the following Scala case class and function definitions:
trait Result[+X,+Y] case class Error[+Y] (s:Y) extends Result[Nothing,Y] case class Ok [+X] (n:X) extends Result[X,Nothing]
def combine[U,X,Y] (us:List[U], f:U=>Result[X,Y]) : Result[List[X],List[Y]] = { us match { case Nil => Ok (Nil) case v::vs => (f (v), combine (vs, f)) match { case (Error (s), Error (ss)) => Error (s::ss) case (Ok (n), Error (ss)) => Error (ss) case (Error (s), Ok (ns)) => Error (List (s)) case (Ok (n), Ok (ns)) => Ok (n::ns) } } }
What is the output of running:
combine (List(1,2,3), (n:Int) => if (n%2==0) Ok (n) else Error (n))
Question : Error(1):Result[List[Int],List[Int]] Ok(List(1,3)):Result[List[Int],List[Int]] This expression does not typecheck, so is rejected by the compiler. Error(List(1,3)):Result[List[Int],List[Int]] Error(List(1,2,3)):Result[List[Int],List[Int]]
5.
Consider the following Scala case class and function definitions:
trait Result[+X,+Y] case class Error[+Y] (s:Y) extends Result[Nothing,Y] case class Ok [+X] (n:X) extends Result[X,Nothing]
def combine[U,X,Y] (us:List[U], f:U=>Result[X,Y]) : Result[List[X],List[Y]] = { us match { case Nil => Ok (Nil) case v::vs => (f (v), combine (vs, f)) match { case (Error (s), Error (ss)) => Error (s::ss) case (Ok (n), Error (ss)) => Error (ss) case (Error (s), Ok (ns)) => Error (List (s)) case (Ok (n), Ok (ns)) => Ok (n::ns) } } }
What is the output of running:
combine (List(0,2,4), (n:Int) => if (n%2==0) Ok (n) else Error (n))
Question : Ok(List(0)) : Result[List[Int],List[Int]] Ok(List(0, 2, 4)) : Result[List[Int],List[Int]] Ok(List(2)) : Result[List[Int],List[Int]] This expression does not typecheck, so is rejected by the compiler. Error(List(0, 2, 4)) : Result[List[Int],List[Int]]
6.
Consider the following Scala case class and function definitions:
trait Result[+X,+Y] case class Error[+Y] (s:Y) extends Result[Nothing,Y] case class Ok [+X] (n:X) extends Result[X,Nothing]
def combine[U,X,Y] (us:List[U], f:U=>Result[X,Y]) : Result[List[X],List[Y]] = { us match { case Nil => Ok (Nil) case v::vs => (f (v), combine (vs, f)) match { case (Error (s), Error (ss)) => Error (s::ss) case (Ok (n), Error (ss)) => Error (ss) case (Error (s), Ok (ns)) => Error (List (s)) case (Ok (n), Ok (ns)) => Ok (n::ns) } } }
The Result type is used to provide either an Ok (indicating successful completion with an associated value) or Error (indicating an error with an associated value). The combine function applies a function f to every element of a list, where f may either succeed or fail. Which description best matches the behavior of the combine function? Question: Returns a collection of all of the errors when any error occurs, even if there are some successes. The combine function does not typecheck, and so is rejected by the Scala compiler. Returns a collection of all of the successes when any success occurs, even if there are some errors. Returns separate collections of all the successes and errors.
7.
The following Scala function is tail-recursive:
def f [X] (xs:List[X], ys:List[X]) : List[X] = { xs match { case Nil => ys case z::zs => z::(f (zs, ys)) } }
Question: True False 8.
The following Scala function is tail-recursive:
def f [X] (xs:List[X]) : List[X] = { xs match { case Nil => Nil case y::ys => f (ys) ::: List (y) } }
Question : True False 9.
The following Scala function is tail-recursive:
def f [X] (xs:List[X], ys:List[X]) : List[X] = { xs match { case Nil => ys case z::zs => f (zs, z::ys) } }
Question : True False
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started