Q:

Set counter in haskell and increment it

digits :: Int->Int->Int
digits x y
     |x`mod`10 == y`mod`10 = count x y 
     |otherwise = digits (x`div`10) (y`div`10)

count :: Int -> Int -> Int
k = 0 + 1
count x y 
    |x < 1 = getprize k
    |otherwise = digits (x`div`10) (y`div`10) 

getprize :: Int -> Int
getprize k
      |k == 8 = 1000000
      |k == 7 = 100000
      |k == 6 = 8000
      |k == 5 = 300       
      |k == 4 = 20
      |k == 3 = 5
      |k == 2 = 1
      |k < 2 = k
      |otherwise = 43 

So I wrote this code in Haskell and the goal is to search how many digits in the input numbers x, y are equal and when I try to set a counter (in my code k) I can't call getprize with k cause it is registered as an integer how can I set the counter to be int so I can count how many digits are the same?

A:

I can't say I understand what you are trying to do in your code, but I think I can address a fundamental misunderstanding arising from your title question ("Set counter in haskell and increment it")
All values in Haskell are immutable, which means they can't be changed after they are set. When you write k = 0 + 1, that means that for the rest of eternity, whenever someone sees k they know for certain that it really means 0 + 1, which is of course equal to 1. Because of this, you can't make the value of k change for each iteration of your function.
Here is an example of using recursion to simulate iteration in Haskell. This function will take a number and add up all the numbers less than or equal to that number all the way down to 0:
sumLessThan :: Int -> Int
sumLessThan 0 = 0
sumLessThan k = k + sumLessThan (k - 1)

Now if I evaluate sumLessThan 3, we can see use the second function definition and substitute 3 for k to get sumLessThan 3 = 3 + sumLessThan (3 - 1). If we keep going, we eventually find the actual result:
sumLessThan 3
3 + sumLessThan (3 - 1)
3 + sumLessThan 2
3 + (2 + sumLessThan (2 - 1))
3 + (2 + sumLessThan 1)
3 + (2 + (1 + sumLessThan 0))
3 + (2 + (1 + 0))
3 + (2 + 1)
3 + 3
6

As you can see, we aren't keeping track of a separate value and computing the result based on that, we are computing the result in terms of smaller results, getting smaller and smaller until we reach our base case, which is a trivial case that is easy to calculate. Our base case was sumLessThan 0 = 0
This is the correct pattern to follow when writing "loops" in Haskell. You should not attempt to use a separate "iterator" value.

