рассчитать среднее значение на языке миранда
Мне действительно нужна помощь в поиске способа вычислить среднее значение на языке миранда. Кажется, я получаю эту ошибку, что он не может объединить тип [num] -> num с num. Кажется, я не могу принять сумму списка как значение и сделать деление?
elements = 4
grades = [24, 12, 33, 17]
|| The code belows purpose is to get the sum of the list
ngrades == [num]
sumlist :: ngrades -> num
sumlist [] = 0
sumlist (front : rest) = front + sumlist rest
|| We calculate the average by getting the sum we calc above to the elements
avg = sumlist div elements
|| We compare each element reccursively to find the maximum value
maxi :: ngrades -> num
maxi [] = 0
maxi (front : []) = front[][1]
maxi (front : next : rest) = maxi (front : rest), if front > next
= maxi (next : rest), otherwise
|| We compare each element reccursively to find the minimum value
mini :: ngrades -> num
mini [] = 0
mini (front : []) = front
mini (front : next : rest) = mini (front : rest), if front < next
= mini (next : rest), otherwise
[1]: https://i.stack.imgur.com/2pYCq.jpg
1 ответ
Вам подходит следующий код?
среднее [] = ошибка "** Ой! **"
среднее xs = сумма xs / #xs
оценки = [24, 12, 33, 17]
результат = средние оценки