I am trying to build a function that converts a Decimal(Int) into a Binary number. Unfortunately other than in java it is not possible to divide an int by two in haskell. I am very new to functional programming so the problem could be something trivial. So far I could not find another solution to this problem but here is my first try :
fromDecimal :: Int -> [Int]
fromDecimal 0 = [0]
fromDecimal n = if (mod n 2 == 0) then
do
0:fromDecimal(n/2)
else
do
1:fromDecimal(n/2)
I got an java implementation here which I did before :
public void fromDecimal(int decimal){
for (int i=0;i<values.length;i++){
if(decimal % 2 = 0)
values[i]=true ;
decimal = decimal/ 2;
else {values[i]= false;
} }
}
Hopefully this is going to help to find a solution!
do's.dos are used for monads. Although lists are instances of theMonadtypeclass, here these are not necessary. Another problem is that you usen/2which means thatnshould beFractional, to perform integer division, usediv.dofor lists is OK. Not ok is usingdofor single expressions, it is totally redundant