Re: [Haskell-cafe] int to bin, bin to int

2007-09-28 Thread Brent Yorgey
On 9/27/07, PR Stanley [EMAIL PROTECTED] wrote: Hi intToBin :: Int - [Int] intToBin 1 = [1] intToBin n = (intToBin (n`div`2)) ++ [n `mod` 2] binToInt :: [Integer] - Integer binToInt [] = 0 binToInt (x:xs) = (x*2^(length xs)) + (binToInt xs) Any comments and/or criticisms on the above

Re: [Haskell-cafe] int to bin, bin to int

2007-09-27 Thread Don Stewart
prstanley: Hi intToBin :: Int - [Int] intToBin 1 = [1] intToBin n = (intToBin (n`div`2)) ++ [n `mod` 2] binToInt :: [Integer] - Integer binToInt [] = 0 binToInt (x:xs) = (x*2^(length xs)) + (binToInt xs) Any comments and/or criticisms on the above definitions would be appreciated.

Re: [Haskell-cafe] int to bin, bin to int

2007-09-27 Thread Christopher L Conway
On 9/27/07, PR Stanley [EMAIL PROTECTED] wrote: Hi intToBin :: Int - [Int] intToBin 1 = [1] intToBin n = (intToBin (n`div`2)) ++ [n `mod` 2] binToInt :: [Integer] - Integer binToInt [] = 0 binToInt (x:xs) = (x*2^(length xs)) + (binToInt xs) Any comments and/or criticisms on the above

Re: [Haskell-cafe] int to bin, bin to int

2007-09-27 Thread Rodrigo Queiro
If you don't like explicit recursion (or points): intToBin = map (`mod` 2) . takeWhile (0) . iterate (`div` 2) binToInt = foldl' (\n d - n*2+d) 0 or even: binToInt = foldl' ((+).(*2)) 0 On 27/09/2007, PR Stanley [EMAIL PROTECTED] wrote: Hi intToBin :: Int - [Int] intToBin 1 = [1] intToBin n

Re: [Haskell-cafe] int to bin, bin to int

2007-09-27 Thread Dan Weston
I might be inclined to use data Bin = Zero | One (or at least type Bin = Bool) to let the type system guarantee that you'll only ever have binary digits in your [Bin], not any old integer. Using [Int] is an abstraction leak, inviting people to abuse the representation behind your back.