-1

I am trying to create a haskell function that will take in an integer and returns a a number in binary.

num2bits :: Integral a => a -> [Char]
num2bits 0 = ['0'] num2bits n | n `mod` 2 == 1 = num2bits (n `div` 2) ++ ['1'] | n `mod` 2 == 0 = num2bits (n `div` 2) ++ ['0']
Will Ness
  • 70,110
  • 9
  • 98
  • 181
Alex Smith
  • 51
  • 2
  • It looks like your code is not properly indented. – Willem Van Onsem May 04 '21 at 18:28
  • How is that a typo, if a person didn't type in the whole code correctly, and instead made a systematic mistake? – Will Ness May 04 '21 at 18:44
  • @Nicole I've rolled back your edit as it invalidates the answer. this isn't allowed. :) I've previously rolled back my own similar edit for the same reason, as it changes the very essence of your question.... or was it really just a typo? – Will Ness May 04 '21 at 18:45
  • Similar to [SO question 67178663](https://stackoverflow.com/questions/67178663/decimal-integer-to-base4-string-haskell). The spurious leading zero can be avoided using an auxiliary function that returns an empty string for zero input. – jpmarinier May 04 '21 at 21:12

1 Answers1

1

Your code is improperly formatted.

Once you correct this, it works (except for always including a superfluous leading '0').

Each clause of a definition should start on its own line, all of them at the same indentation level. All the guards of a clause must be indented more than the clause itself.

Will Ness
  • 70,110
  • 9
  • 98
  • 181