Question: Need help with Haskell Coding, please. Please provide code with the following requirements Question 2 The mean of a set X of numbers is defined

Need help with Haskell Coding, please.

Please provide code with the following requirements

Need help with Haskell Coding, please. Please provide code with the following

Question 2 The mean of a set X of numbers is defined as U xex The standard deviation is defined as (x - )? X In this question you are asked to implement a function basicStats which takes a list of Doubles as argument and returns a pair of Doubles, the first one the mean of the elements in the list, the second their standard deviation: basicStats :: [Double] -> (Double, Double) Since the formula for the standard deviation depends on the mean and on the length of the list, you will need to assign the mean and the length of the list to local variables of the basicStats function (defined in a where or let block), which you can both return as part of the result of basicStats and refer to while calculating the standard deviation. Calculating the mean is easy: Haskell's standard library contains a sum function that sums the elements in a list, and a length function that calculates the length of a list. Use them to calculate the mean. For calculating the standard deviation, the exponentiation operator (**) and the square root function sqrt will be handy, along with the sum function again: >>>> 3 ** 2 9.0 >>> sqrt 9 3.0 To calculate the sum Exex(x-w), you can either use a list comprehension or the map function (my sneaky way to make you search on Hoogle again). An added little wrinkle: Calculating the mean by dividing the sum of the list elements by the length of the list fails if the list is empty. Implement your function so it returns (0,0) for the empty list: >>> :1 question2.hs [1 of 1] Compiling Main ( question2.hs, interpreted ) Ok, one module loaded. >>> basicStats [] (0.0,0.0) >>> basicStats [1..10] (5.5,2.8722813232690143) Another little wrinkle: Summing the elements of the list returns a Double (because the list elements are Doubles). The length of the list is of type Int. Haskell does not allow you to divide a Double by an Int. The types of the two arguments must be of the same type, and it must be a fractional type. In 2 order to be able to divide the sum of the list elements by the length of the list, you need to convert the length of the list to a Double. fromIntegral is the conversion function you want: >>> x = 3 :: Double >>> y = 2 :: Int >>> x/y :17:5: error: Couldn't match expected type 'Double' with actual type 'Int' In the second argument of '(/)', namely 'y' In the expression: x/y In an equation for 'it': it = x/y >>> X / fromIntegral y 1.5 Question 2 The mean of a set X of numbers is defined as U xex The standard deviation is defined as (x - )? X In this question you are asked to implement a function basicStats which takes a list of Doubles as argument and returns a pair of Doubles, the first one the mean of the elements in the list, the second their standard deviation: basicStats :: [Double] -> (Double, Double) Since the formula for the standard deviation depends on the mean and on the length of the list, you will need to assign the mean and the length of the list to local variables of the basicStats function (defined in a where or let block), which you can both return as part of the result of basicStats and refer to while calculating the standard deviation. Calculating the mean is easy: Haskell's standard library contains a sum function that sums the elements in a list, and a length function that calculates the length of a list. Use them to calculate the mean. For calculating the standard deviation, the exponentiation operator (**) and the square root function sqrt will be handy, along with the sum function again: >>>> 3 ** 2 9.0 >>> sqrt 9 3.0 To calculate the sum Exex(x-w), you can either use a list comprehension or the map function (my sneaky way to make you search on Hoogle again). An added little wrinkle: Calculating the mean by dividing the sum of the list elements by the length of the list fails if the list is empty. Implement your function so it returns (0,0) for the empty list: >>> :1 question2.hs [1 of 1] Compiling Main ( question2.hs, interpreted ) Ok, one module loaded. >>> basicStats [] (0.0,0.0) >>> basicStats [1..10] (5.5,2.8722813232690143) Another little wrinkle: Summing the elements of the list returns a Double (because the list elements are Doubles). The length of the list is of type Int. Haskell does not allow you to divide a Double by an Int. The types of the two arguments must be of the same type, and it must be a fractional type. In 2 order to be able to divide the sum of the list elements by the length of the list, you need to convert the length of the list to a Double. fromIntegral is the conversion function you want: >>> x = 3 :: Double >>> y = 2 :: Int >>> x/y :17:5: error: Couldn't match expected type 'Double' with actual type 'Int' In the second argument of '(/)', namely 'y' In the expression: x/y In an equation for 'it': it = x/y >>> X / fromIntegral y 1.5

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!