Home > Back-end >  Input: two natural numbers n, m ∈ N₀ - Output: Number of different digits of the decimal representat
Input: two natural numbers n, m ∈ N₀ - Output: Number of different digits of the decimal representat

Time:12-08

I got the task with the following informations:

  • Input: two natural numbers n, m ∈ N₀
  • Output: Number of different digits of the decimal representations of n and m

Input: two natural numbers n,k Output: digit at the 2^k digit of the binary representation of n

My idea was that:

stelle :: Int -> Int -> Int
stelle n 0 = mod n 2
stelle n k = stelle (div n 2) (k-1)
stelle n k = n k <= O = error

With the last line, haskell shows that there is a mistake. With the line is want to shwo that n and k must be natural numbers, so if n, k <= 0 theh the code is not avaible. Can somebody help me by that?

My second try is this:

stelle :: Int -> Int -> Int                                  
stelle n 0 = mod n 2                                         
stelle n k
| n < 0 && k < 0 = error "Vorraussetzung nicht erfüllt" 
| n >= 0 && k >= 0 = (div n 2) (k-1)

CodePudding user response:

You can work with guards like:

stelle :: Int -> Int -> Int
stelle n k | n < 0 || k < O = error "Vorraussetzung nicht erfüllt"
stelle n 0 = mod n 2
stelle n k = stelle (div n 2) (k-1)
  • Related