Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

***Utilizing Scala*** Please do not use while loops or (re)assignment to variables. You must use recursion instead. You can use val declarations, but not var

***Utilizing Scala***

Please do not use while loops or (re)assignment to variables. You must use recursion instead. You can use "val" declarations, but not "var" declarations.

You may declare auxiliary functions.

Ex. 1:

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] = { // TODO: Provide definition here. null }

Ex. 2:

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] = { // TODO: Provide definition here. null }

Ex. 3:

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)] = { // TODO: Provide definition here. null }

Ex. 4:

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]) = { // TODO: Provide definition here. null }

Ex. 5:

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 = { // TODO: Provide definition here. false }

Please show screenshot of working output!

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

Database Design And SQL For DB2

Authors: James Cooper

1st Edition

1583473572, 978-1583473573

More Books

Students also viewed these Databases questions

Question

Use the conditional operator to write code to check if num

Answered: 1 week ago

Question

What is dividend payout ratio ?

Answered: 1 week ago

Question

Explain the factors affecting dividend policy in detail.

Answered: 1 week ago

Question

=+ Who do you think is right? Why?

Answered: 1 week ago