Answered step by step
Verified Expert Solution
Question
1 Approved Answer
All Code Should be Written in SCALA Problem 2 (20 points) A (5 points) We defined lists in the class. Write a recursive procedure to
All Code Should be Written in SCALA
Problem 2 (20 points) A (5 points) We defined lists in the class. Write a recursive procedure to get the nth element of the list or throw an IllegalArgumentException if n = length of list. Assume n = 0 obtains the very first element and n = length of list - 1 yields very last element. 1 sealed trait NumList case object Nil extends NumList case class Cons(n: Int, l: NumList) extends NumList def getNthElement(lst: NumList, n: Int): Int = { ??? // YOUR CODE HERE B (7 points) Write a recursive procedure that returns true if the list has the Fibonacci property. I.e, every element at position i > 2 is the sum of the two preceding elements. Note that the property is trivially true for lists of sizes 0 and 1. 1 def isFibonacciList (lst: NumList): Boolean = { ??? // YOUR CODE HERE 3 } C (8 points) Write a recursive function filterNumList(l: NumList, f: Int => Boolean): NumList that takes in a NumList and a function f: Int => Boolean. 1. It should return a new list that consist of all elements of the list i that return true when the function f is called on them. 2. The returned list elements must preserve the same order as in the original list. 1 ??? // YOUR CODE HEREStep 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