How to resolve the algorithm Averages/Arithmetic mean step by step in the Haskell programming language
How to resolve the algorithm Averages/Arithmetic mean step by step in the Haskell programming language
Table of Contents
Problem Statement
Write a program to find the mean (arithmetic average) of a numeric vector. In case of a zero-length input, since the mean of an empty set of numbers is ill-defined, the program may choose to behave in any way it deems appropriate, though if the programming language has an established convention for conveying math errors or undefined values, it's preferable to follow it.
Let's start with the solution:
Step by Step solution about How to resolve the algorithm Averages/Arithmetic mean step by step in the Haskell programming language
The Haskell code calculates the mean (average) of a list of numbers.
The mean
function takes a list of fractional numbers and returns a fractional number. If the input list is empty, the function returns 0. Otherwise, it returns the sum of the numbers in the list divided by the length of the list.
The meanReals
function is similar to the mean
function, but it takes a list of real numbers and returns a fractional number. It uses the mean
function to calculate the mean of the real numbers, but first converts the real numbers to fractional numbers using the realToFrac
function.
The third definition of the mean
function uses the foldl'
function to calculate the sum and length of the list in a single pass. It takes a list of real numbers and returns a fractional number. The foldl'
function takes a function, an initial value, and a list, and applies the function to each element in the list, passing the result of the previous application as the first argument to the next application. In this case, the function is defined as a lambda expression that takes the current sum and length of the list, and returns a tuple of the new sum and length after adding the current element to the sum and incrementing the length. The initial value is a tuple of 0 and 0.
The main
function calls the mean
function with a list of numbers from 1 to 100, and prints the result.
Source code in the haskell programming language
mean :: (Fractional a) => [a] -> a
mean [] = 0
mean xs = sum xs / Data.List.genericLength xs
meanReals :: (Real a, Fractional b) => [a] -> b
meanReals = mean . map realToFrac
{-# LANGUAGE BangPatterns #-}
import Data.List (foldl') --'
mean
:: (Real n, Fractional m)
=> [n] -> m
mean xs =
let (s, l) =
foldl' --'
f
(0, 0)
xs
in realToFrac s / l
where
f (!s, !l) x = (s + x, l + 1)
main :: IO ()
main = print $ mean [1 .. 100]
You may also check:How to resolve the algorithm Morse code step by step in the Elixir programming language
You may also check:How to resolve the algorithm Write language name in 3D ASCII step by step in the Erlang programming language
You may also check:How to resolve the algorithm Leap year step by step in the PostScript programming language
You may also check:How to resolve the algorithm Matrix-exponentiation operator step by step in the Rust programming language
You may also check:How to resolve the algorithm Matrix multiplication step by step in the Maxima programming language