 # Finding the number of mutual digits in two integers

Hello! Im new to haskell. I mainly code in java so haskell is a bit hard for me.
How can I make a program that takes two integers as input and returns the number of mutual digits on every position?
for example: program 1234 1789 with output:1
program 437988 137298 with output:3
program 1234 1234 with output:4
As I am used to java Im thinking of making two lists from these two integers and then cheking with a loop which digits are the same, and then returning the counter.But I know there are no loops in haskell and no variables…Any advise from you whould be helpful.

A useful function might be:

``````λ> :t zipWith
zipWith :: (a -> b -> c) -> [a] -> [b] -> [c]
``````

Or use a simple recursion.

There is a good explanation of recursion in Haskell on this website.

In this case I think the main tricky part is the conversion of an integer to a list of digits. First of all: what is a digit? I will use the unicode character that represents the digit, but you could also use integers. So the type of the function we want to define is

``````digits :: Int -> [Char]
``````

A problem that you will probably run into is that it is easier to construct the list of digits in reverse order, e.g. `digits 100 = ['0','0','1']`. I suggest you just define a `digitsReversed` function and use the `reverse` function to put the digits back in the right order. The general structure of your code could look like this:

``````digitsReversed :: Int -> [Char]
digitsReversed n
| <some condition> = <base case>
| otherwise = <recursive step (use digitsReversed here)>
-- These vertical bars are called guards. It means the same as:
-- if <some condition> then <base case> else <recursive step>

digits :: Int -> [Char]
digits n = reverse (digitsReversed n)

mutualDigits :: Int -> Int -> Int
mutualDigits n m = <use digits here>
``````
1 Like