What is the result of code execution?
def dropWhile[A](l: List[A], f: A => Boolean): List[A] =
  l match {
    case Cons(h, t) if f(h) => dropWhile(t, f)
    case _ => l
  }

dropWhile(Nil, (x: Int) => x > 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]
Explanation
dropWhile removes elements from the List prefix as long as they match a predicate.
Nil is an empty List and doesn’t match first expression with Cons(h, t). It matched the second part, which accepts anything and returns current list, which is Nil.

Source: Scala Exercises: functional data structures

Follow CodeGalaxy

Mobile Beta

Get it on Google Play
Send Feedback
Keep exploring
Scala quizzes
Cosmo
Sign Up Now
or Subscribe for future quizzes