Understanding the sum Function#

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

Contents


More Related Answers

  • how to use a sum in where clause sql
  • excel sum
  • sum() function in Python
  • SQL SUM() Function
  • check sum program
  • sum method in python
  • sum values
  • int sum.
  • sum()
  • sum function in python
  • summation of numbers using function
  • Sum using curring
  • Sum using curring
  • sumFunction
  • Ways to write n as sum
  • suma = 0 for i in range(10): suma = suma + i print(suma)
  • Sum of all arguments, SumAll()
  • summarise functions
  • Modifying the sum Function#
  • Modifying the Summation Functions#
  • stats html css
  • sum function python

  • Understanding the sum Function#

    0

    If the above code appears a bit intimidating, don’t worry. Let’s go over it step by step.

    Line 1 is defining the function sum.

    svg viewer

    The dark green section of the illustration above represents the parameters of the function sum which is a single parameter that takes a function which further takes a single parameter of type Int and returns a value of type Int.

    The red section of the illustration represents the return value of sum which is another function. sum returns a function which takes two parameters, both of type Int, and returns a value of type Int.

    From line 2 onwards, we have the body of the function sum. This starts with a definition of a nested function sumHelper. sumHelper takes 2 parameters, a and b (the upper and lower bounds) and returns a value of type Int. The function body of sumHelper is identical to the function body of the previous sum function.

    Line 5 is simply returning the sumHelper function.

    In conclusion, sum is now a function that returns another function; particularly a locally defined or nested function.

    Modifying the Summation Functions

    Below shows how the summation functions would now be redefined. Notice how the parameters of the functions aren’t being specified anymore. This being said, when we call the functions, we still pass the upper and lower bounds as parameters.

    Popularity 7/10 Helpfulness 1/10 Language scala
    Source: Grepper
    Tags: scala sum
    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.