Reputation:
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
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