user20676598
user20676598

Reputation:

Binary representation recursion Haskell

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 
Stelle n k 

But now I have the problem that I have no idea how to deal with it. It has something to do with recursion and div/mod but I dont what the start of the recursion is. Can somebody help me please! Thank you

Upvotes: 0

Views: 142

Answers (1)

willeM_ Van Onsem
willeM_ Van Onsem

Reputation: 476614

The signature should be:

stelle :: Int -> Int -> Int 
stelle n k = …

as for the base case: when k = 0, then you can return n modulo two, for the recursive case, you divide n by two and subtract one from k, so:

stelle :: Int -> Int -> Int 
stelle n 0 = …
stelle n k = …

where I leave implementing the parts as an exercise.

Upvotes: 1

Related Questions