Saturday 4 June 2016

Haskell: How many are equal in three numbers

For the input
10 20 30: 0 numbers are equal
10 20 20: 2 numbers are equal
10 20 10: 2 numbers are equal
10 10 10: 3 numbers are equal


Sample.hs
howManyEqual :: Int -> Int -> Int -> Int
howManyEqual x y z
    | (x == y) && (x == z) = 3
    | (x == y) || (x == z) || (y == z) = 2
    | otherwise = 0

Prelude> :load Sample.hs
[1 of 1] Compiling Main             ( Sample.hs, interpreted )
Ok, modules loaded: Main.
*Main> 
*Main> howManyEqual 10 20 30
0
*Main> howManyEqual 10 20 20
2
*Main> howManyEqual 10 20 10
2
*Main> howManyEqual 10 10 10
3


Previous                                                 Next                                                 Home

No comments:

Post a Comment