extractDigits :: Int -> [Int] extractDigits num | num == 0 = [] | otherwise = extractDigits (num `div` 10) ++ [num `mod` 10] noRepeatingDigits :: Int -> Bool --noRepeatingDigits num = -- -- head d /= d!!1 && head d /= d!!2 && head d /= d!!3 && -- d!!0 /= d!!1 && d!!0 /= d!!2 && d!!0 /= d!!3 && -- d!!1 /= d!!2 && d!!1 /= d!!3 && -- d!!2 /= d!!3 -- where d = extractDigits num noRepeatingDigits num = noRepeats (extractDigits num) noRepeats :: [Int] -> Bool noRepeats [] = True noRepeats (x:xs) = not (x `elem` xs) && (noRepeats xs) -- noRepeatingDigits 1234 -- noRepeatingDigits 1231 numBulls :: Int -> Int -> Int numBulls num1 num2 = if length d1 == length d2 && noRepeatingDigits num1 && noRepeatingDigits num2 then numBullsHelper d1 d2 else -1 where d1 = extractDigits num1 d2 = extractDigits num2 numBullsHelper :: [Int] -> [Int] -> Int numBullsHelper [] [] = 0 numBullsHelper (x:xs) (y:ys) = if x == y then 1 + numBullsHelper xs ys else numBullsHelper xs ys -- numBulls 1234 1235 numCows :: Int -> Int -> Int numCows num1 num2 = if length d1 == length d2 && noRepeatingDigits num1 && noRepeatingDigits num2 then numCowsHelper d1 d2 - numBullsHelper d1 d2 else -1 where d1 = extractDigits num1 d2 = extractDigits num2 numCowsHelper :: [Int] -> [Int] -> Int numCowsHelper [] _ = 0 numCowsHelper (x:xs) y = if x `elem` y then 1 + numCowsHelper xs y else numCowsHelper xs y -- numCows 1234 4326