Quizzes
Site Language: English
Українська
English
Русский
Programming Tests
Login
Sign Up
Programming Tests
Theory
Snippets
Papers
Landing
Android
Prices
FAQ
Cosmo Story
Terms and Conditions
Privacy Policy
Cookies Policy
Send Feedback
pattern matching
:
Content language: English
Русский
Pattern Matching of Sealed Classes
pattern matching
@switch annotatition
pattern matching
What will be printed into the console? val Constant = 'Q' def tokenMe(ch: Char) = (ch: @switch) match { case ' ' | '\t' | '\n' => 1 case 'A' | 'Z' | '$' => 2 case '5' | Constant => 3 case _ => 4 } println(tokenMe('5'))
pattern matching
Examine the next Scala match expression. What will be the result of pattern matching of the list? val x = List(1, 2, 3, 4, 5) match { case Cons(x, Cons(2, Cons(4, _))) => x case Nil => 42 case Cons(x, Cons(y, Cons(3, Cons(4, _)))) => x + y case Cons(h, t) => h + sum(t) case _ => 101 } Assuming the following code is available for your reference sealed trait List[+A] case object Nil extends List[Nothing] case class Cons[+A](head: A, tail: List[A]) extends List[A]
pattern matching
Take a look at the implementation of the following function, select the correct result: def someFunction[A](list: List[A]): List[A] = list match { case Nil => sys.error("empty list") case Cons(_, t) => t } someFunction(List(1, 2, 3)) Assuming the following code is available for your reference sealed trait List[+A] case object Nil extends List[Nothing] case class Cons[+A](head: A, tail: List[A]) extends List[A]
pattern matching
Take a look at the implementation of the following function, select the correct result: def someFunction[A](list: List[A]): List[A] = list match { case Nil => sys.error("empty list") case Cons(_, t) => t } someFunction(List(1)) Assuming the following code is available for your reference sealed trait List[+A] case object Nil extends List[Nothing] case class Cons[+A](head: A, tail: List[A]) extends List[A]
pattern matching
Take a look at the implementation of the following function, select the correct result: def someFunction[A](l: List[A], h: A): List[A] = l match { case Nil => sys.error("empty list") case Cons(_, t) => Cons(h, t) } someFunction(List(1, 2, 3), 3) Assuming the following code is available for your reference sealed trait List[+A] case object Nil extends List[Nothing] case class Cons[+A](head: A, tail: List[A]) extends List[A]
pattern matching
Given drop function implemented with pattern matching, what is the result of the code execution? def drop[A](l: List[A], n: Int): List[A] = if (n <= 0) l else l match { case Nil => Nil case Cons(_, t) => drop(t, n - 1) } drop(List(1, 2, 3), 1) Assuming the following code is available for your reference sealed trait List[+A] case object Nil extends List[Nothing] case class Cons[+A](head: A, tail: List[A]) extends List[A]
pattern matching
Select correct implementation of drop function, so that following expression is true. drop(List(1, 2, 3), 1) == List(2,3) Assuming the following code is available for your reference sealed trait List[+A] case object Nil extends List[Nothing] case class Cons[+A](head: A, tail: List[A]) extends List[A]
pattern matching
What is the result of code execution? def drop[A](l: List[A], n: Int): List[A] = if (n <= 0) l else l match { case Nil => Nil case Cons(_, t) => drop(t, n - 1) } drop(List(1, 2, 3), 0) Assuming the following code is available for your reference sealed trait List[+A] case object Nil extends List[Nothing] case class Cons[+A](head: A, tail: List[A]) extends List[A]
pattern matching
← Prev
1
2
Next →
Sign Up Now
or
Subscribe for future quizzes