Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Haskell - The Craft of Functional Programming (exercise 4.3)

I have the following question (Haskell - The Craft of Functional Programming):

Give a definition of the function

howManyEqua1 :: Int -> Int -> Int -> Int

which returns how many of its three arguments are equal, so that

howManyEqua1 :: 34 25 36 = 0
howManyEqual :: 34 25 34 = 2
howManyEqual :: 34 34 34 = 3

The answer I gave is:

howManyEqual :: Int -> Int -> Int -> Int
howManyEqual    a      b      c
    | a == b && b == c            = 3
    | a == b                      = 2
    | b == c                      = 2
    | a == c                      = 2
    | otherwise                   = 0

However, I believe there is a better way to classify it but am not sure of how.

like image 581
maclunian Avatar asked Dec 01 '22 02:12

maclunian


1 Answers

How about:

howManyEqual a b c
    | a == b && b == c           = 3
    | a /= b && a /= c && b /= c = 0
    | otherwise                  = 2
like image 135
Sean Avatar answered Dec 04 '22 03:12

Sean