PDA

View Full Version : Basic Haskell Question - Just learning


weedweaver
10-03-2003, 02:26 PM
hey i have wrote a very simple function that computes the average of three inputted integers. how do i write a function that computes how many integers are above the average using the average function? thanks for any help.

sicarius
10-03-2003, 03:10 PM
This sounds very similar to a problem from "The Craft of Functional Programming, second Edition". Therefore I am going to assume it is home work and not provide the actual solution, but here is something to think about.

One solution might be to write a seperate function that determines whether a single number is above a given average, the function would return 1 if the number is above and 0 otherwise.

This is all I'm going to say, there are many other solutions. Have fun.

weedweaver
10-03-2003, 03:19 PM
Yeah its one of the exercises in the book.. im reading through the book at the mo trying to learn as i go along... shame the book doesnt provide the answers to the exercises so u can check ur answer

weedweaver
10-03-2003, 03:22 PM
But with that seperate function u would have to call it three times to check each input, wouldnt it just return e.g. two 1's (if two are above the average) rather than the number two itself??

sicarius
10-03-2003, 04:06 PM
Yes, but you could sum the returns of each call and return that as your answer. :)

-- Added this a second latter


I am not sure where it is in the book, but to avoid calculating the value of your average function, use a where statement. Here is an example.


averageThree :: Float -> Float -> Float -> Float
averageThree a b c = (a + (b + c))/3.0

isAbove :: Float -> Float -> Int
isAbove a avg
| a > avg = 1
| otherwise = 0

numAbove :: Float -> Float -> Float -> Int
numAbove a b c = (isAbove a avg) + (isAbove b avg) + (isAbove c avg)
where
avg = averageThree a b c




The above code should work (I don't have hugs at work to test it). The where statement is evaluated before the rest of the function, and its value is stored and reused if possible.

In our case the value of avg doesn't change in the function so it is calculated only once.

Hope this helps.

P.S. I am sure there are other, better solutions to this. This is just what came to mind first.

weedweaver
10-03-2003, 04:12 PM
got this at the mo but im having problems with incompatible types

average :: Int -> Int -> Int -> Float
average x y z = fromInt(x + y + z) / 3

howmanyaboveaverage :: Int -> Int -> Int -> Int
howmanyaboveaverage x y z
|fromInt(x || y || z) > average(x y z) = 1
|fromInt(x && y || x && z || y &&z) > average(x y z) = 2

sicarius
10-03-2003, 04:23 PM
From what you posted I can't tell if you code is properly formated.

one of the lines should be similar to


average x y z = (fromInt (x + y + z)) / 3


arguments to functions should be separated by spaces, and parenthesis are very important because of this.

weedweaver
10-03-2003, 04:27 PM
ah thanks mate for the help, very much appreciated!

sicarius
10-03-2003, 04:49 PM
No problem. glad I could help.