Question

In Haskell programming language (1) Write a "averageThree" function to return the average of three integers....

In Haskell programming language
(1) Write a "averageThree" function to return the average of three integers.
averageThree :: Integer -> Integer -> Integer -> Float

(2) Write a "howManyAboveAverage" function which returns how many of three integer inputs are above its average value. (utilize the averageThree function.)
howManyAboveAverage :: Integer -> Integer -> Integer -> Integer

(3) Write a "howManyWithinThreshold" function that returns how many of the first three arguments are within the threshold (the fourth argument) of the average of the first three arguments. (utilize the averageThree function and the logic in the howManyAboveAverage function.)
howManyWithinThreshold :: Integer -> Integer -> Integer -> Float -> Integer

I personally have completed (1) and (2)!

[sample of (1)]
averageThree :: Integer -> Integer -> Integer -> Float
averageThree a b c = (fromIntegral (a + b + c)) / 3

All i need help with is 3, where i am stuck and can not resolve!

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Source Code:

module main where

--averageThree to return the average of three integers
averageThree :: Integer -> Integer -> Integer -> Float
averageThree x y z = fromInteger(x*y*z)/3

--howManyAboveAverage that returns how many of three integer inputs are above its average value
howManyAboveAverage :: Integer -> Integer -> Integer -> Integer
howManyAboveAverage x y z
| x > round(averageThree x y z) && y > round(averageThree x y z) = 2
| y > round(averageThree x y z) && z > round(averageThree x y z) = 2
| x > round(averageThree x y z) && z > round(averageThree x y z) = 2
| x==y && y==z = 0
| otherwise = 1

--howManyWithinThreshold that returns how many of the first three arguments are within the threshold
--(the fourth argument) of the average of the first three arguments.
howManyWithinThreshold :: Integer -> Integer -> Integer -> Float -> Integer
howManyWithinThreshold w x y z
| ((averageThree w x y)-z) < fromInteger(x) && ((averageThree w x y)+z) > fromInteger(x) && ((averageThree w x y)-z) < fromInteger(y) && ((averageThree w x y)+z) > fromInteger(y) && ((averageThree w x y)-z) < fromInteger(w) && ((averageThree w x y)+z) > fromInteger(w) = 3
| ((averageThree w x y)-z) < fromInteger(x) && ((averageThree w x y)+z) > fromInteger(x) && ((averageThree w x y)-z) < fromInteger(y) && ((averageThree w x y)+z) > fromInteger(y) && ((averageThree w x y)-z) > fromInteger(w) || ((averageThree w x y)+z) < fromInteger(w) = 2
| ((averageThree w x y)-z) < fromInteger(x) && ((averageThree w x y)+z) > fromInteger(x) && ((averageThree w x y)-z) > fromInteger(y) || ((averageThree w x y)+z) < fromInteger(y) && ((averageThree w x y)-z) < fromInteger(w) && ((averageThree w x y)+z) > fromInteger(w) = 2
|((averageThree w x y)-z) > fromInteger(x) || ((averageThree w x y)+z) < fromInteger(x) && ((averageThree w x y)-z) < fromInteger(y) && ((averageThree w x y)+z) > fromInteger(y) && ((averageThree w x y)-z) < fromInteger(w) && ((averageThree w x y)+z) > fromInteger(w) = 2
|((averageThree w x y)-z) < fromInteger(x) && ((averageThree w x y)+z) > fromInteger(x) && ((averageThree w x y)-z) > fromInteger(y) || ((averageThree w x y)+z) < fromInteger(y) && ((averageThree w x y)-z) > fromInteger(w) || ((averageThree w x y)+z) < fromInteger(w) = 1
|((averageThree w x y)-z) > fromInteger(x) || ((averageThree w x y)+z) < fromInteger(x) && ((averageThree w x y)-z) < fromInteger(y) && ((averageThree w x y)+z) > fromInteger(y) && ((averageThree w x y)-z) > fromInteger(w) || ((averageThree w x y)+z) < fromInteger(w) = 1
|((averageThree w x y)-z) > fromInteger(x) || ((averageThree w x y)+z) < fromInteger(x) && ((averageThree w x y)-z) > fromInteger(y) || ((averageThree w x y)+z) < fromInteger(y) && ((averageThree w x y)-z) < fromInteger(w) && ((averageThree w x y)+z) > fromInteger(w) = 1
| otherwise = 0

Hope this helps!

Add a comment
Know the answer?
Add Answer to:
In Haskell programming language (1) Write a "averageThree" function to return the average of three integers....
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT