Question

In: Computer Science

Question: Write a method named reduce that: ● Takes a function of type (A, A) =>...

Question: Write a method named reduce that:
● Takes a function of type (A, A) => A
● Returns A
● Combines all the elements of the list into a single value by applying the provided function
to all elements
○ You may assume the function is commutative
● If the list has size 1, return that element without calling the provided function
Example: If head stores a reference to the List(4, 6, 2)
head.reduce((a: Int, b: Int) => a + b) == 12

Programming language is Scala

Solutions

Expert Solution

Modified Code with explanation in comments:

object ListSum {

def main(args: Array[String]) {
var i=0
var sum=0
//head will store the value of list
val head=List(2)
  
//if size of list=1, then it will directly print the list
//else it will call the function named reduce(Int,Int)==>Int
//return Int
if(head.size==1){
print(head(0))
}
else{
for( i <- 0 to (head.size-2) by 2)
{
sum+=reduce(head(i),head(i+1))
}
if(head.size%2!=0){
sum+=reduce(head(head.size-1),0)
}
print(sum)
}
}
  
//reduce funtion of type (A,A) => A(return type)
def reduce(a:Int,b:Int) : Int =
{
return a+b
}
}

--------------------------------------------------------------------------------------------------------------------------

Screenshot of Output with Code:


Related Solutions

Write a boolean function named isMember that takes two arguments: an array of type char and...
Write a boolean function named isMember that takes two arguments: an array of type char and a value. It should return true if the value is found in the array, or false if the value is not found in the array. PLEASE WRITE FULL PROGRAM IN C++ AND USE RECURSION AND DO NOT USE LOOPS
Write a class named Palindrome.java and Write a method isPalindrome that takes an IntQueue as a...
Write a class named Palindrome.java and Write a method isPalindrome that takes an IntQueue as a parameter and that returns whether or not the numbers in the queue represent a palindrome (true if they do, false otherwise). A sequence of numbers is considered a palindrome if it is the same in reverse order. For example, suppose a Queue called q stores this sequence of values: front [3, 8, 17, 9, 17, 8, 3] back Then the following call: isPalindrome(q) should...
write a function named as cubeCalculator that takes an integer pointer as function and return its...
write a function named as cubeCalculator that takes an integer pointer as function and return its cube value , you are required to compute the cube of a number using pointer notation , return the result as an integer value , use c++
Python Problem 3 Write a function named enterNewPassword. This function takes no parameters. It prompts the...
Python Problem 3 Write a function named enterNewPassword. This function takes no parameters. It prompts the user to enter a password until the entered password has 8-15 characters, including at least one digit. Tell the user whenever a password fails one or both of these tests.
Write a function named findIndex that takes an array of integers, the number of elements in...
Write a function named findIndex that takes an array of integers, the number of elements in the array, and two variables, such that it changes the value of the first to be the index of the smallest element in the array, and changes the value of the second to be the index of the largest element in the array. Please complete this in C++, using pass by reference
Write a function named findIndex that takes an array of integers, the number of elements in...
Write a function named findIndex that takes an array of integers, the number of elements in the array, and two variables, such that it changes the value of the first to be the index of the smallest element in the array, and changes the value of the second to be the index of the largest element in the array. Please complete this in C++
Write a function named "characters" that takes a string as a parameter and returns the number...
Write a function named "characters" that takes a string as a parameter and returns the number of characters in the input string
Write a function named "replacement" that takes a string as a parameter and returns an identical...
Write a function named "replacement" that takes a string as a parameter and returns an identical string except with every instance of the character "w" replaced with the character "v" My code: function replacement(word){ var str=word; var n=str.replace("w","v"); return n; } Syntax Error: function replacement incorrect on input Not sure how to fix? Can't use a loop for answer
a splitting function, split_by Write a splitting function named split_by that takes three arguments an equality...
a splitting function, split_by Write a splitting function named split_by that takes three arguments an equality checking function that takes two values and returns a value of type bool, a list of values that are to be separated, and a list of separators values. This function will split the second list into a list of lists. If the checking function indicates that an element of the first list (the second argument) is an element of the second list (the third...
Write a MATLAB function named numberWords() that takes a whole number as an argument and returns...
Write a MATLAB function named numberWords() that takes a whole number as an argument and returns a string containing the number word for the whole numbers 0 - 999. For example:  numberWords(234) would return 'two hundred thirty-four' If the input value is not a whole number between 0 - 999 then the function should return a string equivalent to 'ERROR'.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT