Haskell. Numbers in binary numbers. words

Posted by Katja on Stack Overflow See other posts from Stack Overflow or by Katja
Published on 2011-01-13T16:50:19Z Indexed on 2011/01/13 16:53 UTC
Read the original article Hit count: 206

Filed under:

Hi! I need to code words into binary numbers. IN: "BCD..." OUT:1011...

I have written already funktion for coding characters into siple numbers

IN: 'C' OUT: 3 IN: 'c' OUT: 3

lett2num :: Char -> Int
lett2num x
| (ord 'A' <= ord x) && (ord x <= ord 'Z') = (ord x - ord 'A') + 1
| (ord 'a' <= ord x) && (ord x <= ord 'z') = (ord x - ord 'a') +1


num2lett :: Int -> Char
num2lett n
| (n <= ord 'A') && (n <= ord 'Z') = chr(ord 'A'+ n - 1)
| (n <= ord 'a') && (n <= ord 'Z') = chr(ord 'A'+ n - 1)

I wrote as well function for codind simple numbers into binary.

num2bin :: Int->[Int]
num2bin 0 = []
num2bin n
| n>=0 = n `mod` 2 : (num2bin( n `div` 2))
| otherwise = error

but I donw want those binary numbers to be in a list how can I get rid of the lists?

Thanks

© Stack Overflow or respective owner

Related posts about haskell