Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

// GENERATED /* INSTRUCTIONS * * Complete the exercises below. For each EXERCISE comment, add * code immediately below the comment. * * Please see

// GENERATED

/* INSTRUCTIONS * * Complete the exercises below. For each "EXERCISE" comment, add * code immediately below the comment. * * Please see README.md for instructions, including compilation and testing. * * GRADING * * 1. Submissions MUST compile using SBT with UNCHANGED configuration and tests with no * compilation errors. Submissions with compilation errors will receive 0 points. * Note that refactoring the code will cause the tests to fail. * * 2. You MUST NOT edit the SBT configuration and tests. Altering it in your submission will * result in 0 points for this assignment. * * 3. You MUST NOT use while loops or (re)assignment to variables (you can use "val" declarations, * but not "var" declarations). You must use recursion instead. * * 4. You may declare auxiliary functions if you like. * * SUBMISSION * * 1. Push your local repository to the repository created for you on Bitbucket before the deadline. * * 2. Late submissions will not be permitted because solutions will be discussed in class. * */

object fp1 {

// EXAMPLE: here is the definition of the factorial function. def fact (n : Int) : Int = { if (n <= 1) 1 else n * fact (n - 1) }

// EXERCISE 5: write a function "swap" that takes a pair of an Int and a String, and returns a // pair of a String and an Int (with the values from the pair passed an argument. E.g., swap (p1) // should return ("hello", 7). You can use "p._1" and "p._2" to access the first and second // components of a pair. def swap (p:(Int,String)) : (String,Int) = { // TODO: Provide definition here. null }

// EXERCISE 6: write a function "sum" that takes a list of integers and sums them. As with all of // the exercises in this assignment, your function MUST be recursive and MUST NOT use a while // loop. def sum (xs : List[Int]) : Int = { // TODO: Provide definition here. -1 }

// EXERCISE 7: given the definition of the function "sumTailRecursiveAux" below, complete the // definition of the function "sumTailRecursive" so that it also sums a list of integers. You // must not alter the definition of "sumTailRecursiveAux". Your definition for "sumTailRecursive" // must call "sumTailRecursiveAux" directly, and must not call "sum" def sumTailRecursiveAux (accumulator : Int, xs : List[Int]) : Int = { xs match { case Nil => accumulator case y::ys => sumTailRecursiveAux (accumulator + y, ys) } }

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

// EXERCISE 8: complete the following definition of the function "max" that finds the maximum // integer in a list of integers. Note that no value can be returned when the list is empty, // hence the "NoSuchElementException". Your function MUST be recursive and MUST NOT use a while // loop. You MUST NOT use the "max" method on lists, but can use the "max" method on integers. def max (xs : List[Int]) : Int = { // TODO: Provide definition here. -1 }

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

Students also viewed these Databases questions