<br><div><span class="gmail_quote">On 9/27/07, <b class="gmail_sendername">PR Stanley</b> <<a href="mailto:email@example.com">firstname.lastname@example.org</a>> wrote:</span><blockquote class="gmail_quote" style="border-left: 1px solid rgb(204, 204, 204); margin: 0pt 0pt 0pt 0.8ex; padding-left: 1ex;">
Hi<br>intToBin :: Int -> [Int]<br>intToBin 1 = <br>intToBin n = (intToBin (n`div`2)) ++ [n `mod` 2]<br><br>binToInt :: [Integer] -> Integer<br>binToInt  = 0<br>binToInt (x:xs) = (x*2^(length xs)) + (binToInt xs)
<br>Any comments and/or criticisms on the above definitions would be appreciated.<br>Thanks , Paul</blockquote><div><br>Others have already given many good suggestions, but I'll add something specifically about the order of the bits in the result. You have the generated list of bits in "reading order",
i.e. high-order bits first. But perhaps it would make more sense to have the low-order bits first? At least, it would be more efficient that way. Then you could do<br><br>intToBin n = (n `mod` 2) : (intToBin (n `div` 2)
<br><br>The way you have it now, you will end up with something like  ++  ++  ++  ++ ... which ends up inefficiently traversing the list multiple times. To undo, just (for example)<br><br>binToInt xs = sum $ zipWith (*) xs (iterate (*2) 1).