Challenge: Write your First Higher-Order Function

Sumit Rawal answered on May 23, 2023 Popularity 1/10 Helpfulness 1/10

Contents


More Related Answers

  • 6. Higher-Order Functions#
  • What are Higher-Order Functions?
  • Solution Review: Write Your First Higher-Order Function

  • Challenge: Write your First Higher-Order Function

    0

    Test Yourself

    Problem Statement

    You need to create a higher-order function arithmeticPrinter which prints the result of an arithmetic function that has two parameters of type Int and returns a value of type Int.

    In this challenge, you will assume that the following arithmetic functions have been declared:

    def add(a: Int, b: Int): Int = {

    a + b

    }

    def subtract(a:Int, b:Int) = {

    a - b

    }

    def multiply(a:Int, b:Int) = {

    a * b

    }

    def divide(a:Int, b:Int) = {

    a / b

    }

    For instance, the arithmeticPrinter will take the add function as input and print its result.

    Input

    arithmeticPrinter has three parameters.

    A function f which has two parameters of type Int and returns a value of type Int.

    An integer x

    An integer y

    The input will be a function and two integers that will be passed to the function.

    Output

    The output will be the result of the arithmetic function.

    Sample Input

    add, 4, 9

    Sample Output

    13

    Test Yourself

    Write your code in the given area. Try the exercise by yourself first, but if you get stuck, the solution has been provided. Good luck! 

    Popularity 1/10 Helpfulness 1/10 Language scala
    Source: Grepper
    Tags: function scala
    Link to this answer
    Share Copy Link
    Contributed on May 23 2023
    Sumit Rawal
    0 Answers  Avg Quality 2/10


    X

    Continue with Google

    By continuing, I agree that I have read and agree to Greppers's Terms of Service and Privacy Policy.
    X
    Grepper Account Login Required

    Oops, You will need to install Grepper and log-in to perform this action.