Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

***Utilizing Scala*** Ex. 1: //Complete the following recursive definition of a foldRight function called myFoldRight for Scala's builtin list type. //You must not use the

***Utilizing Scala***

Ex. 1:

//Complete the following recursive definition of a "foldRight" function called "myFoldRight" for Scala's builtin list type.

//You must not use the builtin "foldRight" method.

//Your implementation of "myFoldRight" MUST be recursive.

//HINT: myFoldRight ( Nil, e, f) == e

//myFoldRight (y::ys, e, f) == f (y, myFoldRight (ys, e, f))

defmyFoldRight[A,B] (xs:List[A], e:B, f:(A,B)=>B) :B={

//TODO: Provide definition here.

e

}

Ex. 2:

//Complete the following definition of a "joinTerminateLeft" function

//to take a list of strings "xs" and concatenate all strings using a string "term"

//as a terminator (not delimiter) between strings.

//You MUST use your myFoldLeft defined above.

//EXAMPLES:

//- joinTerminateLeft (Nil, ";") == ""

//- joinTerminateLeft (List ("a","b","c","d"), ";") == "a;b;c;d;"

importfold_left._

defjoinTerminateLeft(xs: List[String], term: String) :String={

//TODO: Provide definition here.

null

}

Ex. 3:

//Complete the following definition of a "joinTerminateRight" function

//to take a list of strings "xs" and concatenate all strings using a string "term"

//as a terminator (not delimiter) between strings.

//You MUST use your myFoldRight defined above.

//EXAMPLES:

//- joinTerminateRight (Nil, ";") == ""

//- joinTerminateRight (List ("a","b","c","d"), ";") == "a;b;c;d;"

importfold_right._

defjoinTerminateRight(xs: List[String], delimiter: String) :String={

//TODO: Provide definition here.

null

}

Ex. 4:

// Complete the following recursive definition of a "firstNumGreaterThanEqual" function // to find the first number greater than or equal to "a" in a list of integers "xs". // If the list is empty or there is no number greater than or equal to "a", throw a RuntimeException (with no argument). // Your implementation of "firstNumGreaterThanEqual" MUST be recursive. // EXAMPLES: // - firstNumGreaterThanEqual (5, List (4, 6, 8, 5)) == 6

def firstNumGreaterThanEqual (a : Int, xs : List[Int]) : Int = { // TODO: Provide definition here. -1 }

Ex. 5:

// Complete the following recursive definition of a "firstIndexNumGreaterThanEqual" function // to find the index (position) of the first number greater than or equal to "a" in a list of integers "xs". // If the list is empty or there is no number greater than or equal to "a", throw a RuntimeException (with no argument). // The first index should be zero (not one). // Your implementation of "firstIndexNumGreaterThanEqual" MUST be recursive. // EXAMPLES: // - firstIndexNumGreaterThanEqual (5, List (4, 6, 8, 5)) == 1 // HINT: this is a bit easier to write if you use an auxiliary function.

def firstIndexNumGreaterThanEqual (a : Int, xs : List[Int]) : Int = { // TODO: Provide definition here. -1 }

Please include screenshots of working code and its 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_2

Step: 3

blur-text-image_3

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

Object Oriented Databases Prentice Hall International Series In Computer Science

Authors: John G. Hughes

1st Edition

0136298745, 978-0136298748

Students also viewed these Databases questions