• Home
  • Blog
  • Working in very simple Haskell code

Working in very simple Haskell code

0 comments

Implement the sum1 function without help from any standard library functions other than the
standard addition operator. To do this, you need to think recursively: What is the sum of the empty
list of elements? Given a list x:xs with a head x and a tail xs, how can you recursively define the
sum of the elements in this list? This should lead to two equations of the form:
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 op

About the Author

Follow me


{"email":"Email address invalid","url":"Website address invalid","required":"Required field missing"}