Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Checking if code compiles in Scala Please run the following programs to check if they compile. Make any corrections if they do not compile. Please

Checking if code compiles in Scala

Please run the following programs to check if they compile. Make any corrections if they do not compile. Please include screenshots in your response!

Complete the function definition for "delete1" that takes an element "x" and a list "ys", then returns the list where any occurrences of "x" in "ys" have been removed. Your definition of "delete1" MUST be recursive. EXAMPLE: - delete1 ("the", List ("the","the","was","a","product","of","the","1980s")) == List ("was","a","product","of","1980s")

def delete1 [X] (x:X, ys:List[X]) : List[X] = { ys match { case Nil => Nil case head :: tail => if (head == x) delete1(x, tail) else head :: delete1(x, tail) } }

Complete the function definition for "delete2" below. It must have the same behavior as "delete1". It must be written using "for comprehensions" and not use recursion explicitly.

def delete2 [X] (x:X, ys:List[X]) : List[X] = { for (y <- ys if y != x) yield y }

Complete the function definition for "delete3" below. It must have the same behavior as "delete1". It must be written using the builtin "filter" method for Lists and not use recursion explicitly.

def delete3 [X] (x:X, ys:List[X]) : List[X] = { ys.filter(y => y != x) }

Complete the following recursive definition of a "member" function to check whether an element "a" is a member of a list of integers "xs". Your implementation of "member" MUST be recursive and not use the builtin "contains" method from the List class. EXAMPLES: - member (5, List (4, 6, 8, 5)) == true - member (3, List (4, 6, 8, 5)) == false

def member (a : Int, xs : List[Int]) : Boolean = { xs match { case Nil => false case head :: tail => if (head == a) true else member(a, tail) } }

Complete the function definition for "removeDupes1" below. It takes a list as argument, then returns the same list with consecutive duplicate elements compacted to a single element. Duplicate elements that are separated by at least one distinct element should be left alone. EXAMPLE: - removeDupes1 (List (1,1,2,3,3,3,4,4,5,6,7,7,8,9,2,2,2,9)) == List (1,2,3,4,5,6,7,8,9,2,9)

def removeDupes1 [X] (xs:List[X]) : List[X] = { if (xs.isEmpty) { Nil } else { xs.head :: removeDupes1(xs.tail.dropWhile(_ == xs.head)) } }

Write a function "removeDupes2" that behaves like "removeDupes1", but also includes a count of the number of consecutive duplicate elements in the original list (thus producing a simple run-length encoding). The counts are paired with each element in the output list. EXAMPLE: - removeDupes2 (List (1,1,2,3,3,3,4,4,5,6,7,7,8,9,2,2,2,9)) == List ((2,1),(1,2),(3,3),(2,4),(1,5),(1,6),(2,7),(1,8),(1,9),(3,2),(1,9))

def removeDupes2 [X] (xs:List[X]) : List[(Int, X)] = { xs.foldRight(List[(Int, X)]()) { case (x, acc) => acc match { case (count, prev) :: rest if prev == x => (count + 1, prev) :: rest case _ => (1, x) :: acc } } }

Complete the following definition of a function that splits a list into a pair of two lists. The offset for the the split position is given by the Int argument. The behavior is determined by: for all n, xs: splitAt (n, xs) == (take (n, xs), drop (n, xs)) Your definition of "splitAt" must be recursive and must not use "take" or "drop". Your definition of "splitAt" must only travere the list once. So you cannot define your own versions of "take"/"drop" and use them (because that would entail one traversal of the list with "take" and then a second traversal with "drop").

def splitAt [X] (n:Int, xs:List[X]) : (List[X], List[X]) = { def go(n: Int, xs: List[X], acc: List[X]): (List[X], List[X]) = { if (n <= 0) (acc.reverse, xs) else xs match { case Nil => (acc.reverse, Nil) case h :: tail => go(n - 1, tail, h :: acc) } } go(n, xs, Nil) }

Complete the definition of the following function that computes the length of each String in a list, and returns the original Strings paired with their length. For example: stringLengths (List ("the", "rain")) == List (("the", 3), ("rain", 4)) You must not use recursion directly in the function. You can use the "map" method of the List class.

def stringLengths (xs:List[String]) : List[(String, Int)] = { xs.map(x => (x, x.length)) }

Complete the following definition of an "allDistinct" function that checks whether all values in list are distinct. You should use your "member" function defined earlier. Your implementation must be recursive. EXAMPLE: - allDistinct (Nil) == true - allDistinct (List (1,2,3,4,5)) == true - allDistinct (List (1,2,3,4,5,1)) == false - allDistinct (List (1,2,3,2,4,5)) == false

import member._

def allDistinct (xs : List[Int]) : Boolean = { xs match { case Nil => true case head :: tail => if (member(head, tail)) false else allDistinct(tail) } }

Complete the following recursive definition of an "allEqual" function to check whether all elements in a list of integers are equal. EXAMPLES: - allEqual (Nil) == true - allEqual (List (5)) == true - allEqual (List (5, 5, 5)) == true - allEqual (List (6, 5, 5, 5)) == false - allEqual (List (5, 5, 6, 5)) == false - allEqual (List (5, 5, 5, 6)) == false

def allEqual (xs : List[Int]) : Boolean = { xs match { case Nil => true case head :: Nil => true case head :: tail => if (head == tail.head) allEqual(tail) else false } }

Thank you!

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Select Healthcare Classification Systems And Databases

Authors: Katherine S. Rowell, Ann Cutrell

1st Edition

0615909760, 978-0615909769

More Books

Students also viewed these Databases questions

Question

Describe the factors influencing of performance appraisal.

Answered: 1 week ago

Question

1. Explain what is meant by ethical behavior.

Answered: 1 week ago