In the following problems do not use any mutable variables (i.e., variables decl
ID: 3887905 • Letter: I
Question
In the following problems do not use any mutable variables (i.e., variables declared with var) or any mutable predened types such as arrays, mutable sets, and so on. You may dene methods recursively this time. Do not use the if operator in Problems 1-4 below.
3. Write a Scala method isIn (x: Int, l: List[Int]): Boolean which, given an integer value x and an integer list l, returns true if and only if x is an element of l. Do not use the contains method or similar methods of the List class or any other class.
Explanation / Answer
Hi, Please find my implementation.
def isIn (x: Int, l: List[Int]): Boolean = {
l.foreach(ele => if(ele == x) return true)
false // not found
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.