While trying to solve the following exercise...
Define the function
binaryDigits
, that receives an Integer and returns the amount of digits required to write such umber in binary code.
... I found myself thinking that it is impossible to solve this problem by calling the function binaryDigits
recursively because the only variable it receives is the actual number
I say this because the website states that while the tests it runs were succeeded, I didn't use recursion in the function binaryDigits
.
so how could I even think of recursively call this function if I can't even tell how many times have I called it (and lets say that the number of times being called represent how many binary digits it takes to represent that number).
This is what I thought: please note that I'm using recursion but in an auxiliary function that returns a list of the decimal values that binary digits represent when the sum of that list is greater than the received number:
double = (2*)
listOfBinaryDigits list num | num > (sum list) = listOfBinaryDigits ([double(head list)]++list) num
| otherwise = list
binaryDigits num = length (listOfBinaryDigits [1] num)
div 153 10 == 15
? Ordiv 15 10 == 1
? Ordiv 1 10 == 0
(hint: assume you need only 0 digits for 0 itself). – chepner