Hi Sirs / Mam.
I am new to Haskell. I am testing out few things which I learnt.
I was trying to find an average of the following list. And I don’t know how to do it. Kindly help.
type StudentRate = ( String , Int )
If I use this [StudentRate] as a list , how to find the average of the INt value.
Define a list, for example: let studentRates = [("Angela", 3), ("Olaf", 4)]
Use the map function to apply snd to each element. The function snd extracts the second element from each tuple: let rates = map snd studentRates
Calculate the sum of the rates: let sumRates = sum rates
Count the number of elements in the rates list: let countRates = length rates
Divide sumRates by countRates. But before you do that, convert countRates to a floating point number. Otherwise Haskell will complain: sumRates / (fromIntegral countRates)
This solution is admittedly not very elegant, but it allows to see the effect of each individual step.