
Hi, i've one fuction receiving an int . The objective is return the respective binary code of that int. For example, if i receive 28 the fuction will return 011100. My question is that there is any fuction in libraries that do this? If not, anyone has any suggestion? Thank you. -- View this message in context: http://www.nabble.com/Binary-code-tf2704645.html#a7541561 Sent from the Haskell - Haskell-Cafe mailing list archive at Nabble.com.

Hello,
First, and forgive me if I'm making unwarranted assumptions, but
http://haskell.org/haskellwiki/Homework_help might be useful.
Second: div, mod, reverse, and the unfoldr function from Data.List
will do what you want.
/g
On 11/25/06, escafia
Hi,
i've one fuction receiving an int . The objective is return the respective binary code of that int.
For example, if i receive 28 the fuction will return 011100.
My question is that there is any fuction in libraries that do this? If not, anyone has any suggestion?
Thank you. -- View this message in context: http://www.nabble.com/Binary-code-tf2704645.html#a7541561 Sent from the Haskell - Haskell-Cafe mailing list archive at Nabble.com.
_______________________________________________ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe
-- It is myself I have never met, whose face is pasted on the underside of my mind.

On 28.11.2006 01:20 J. Garrett Morris wrote:
Hello,
First, and forgive me if I'm making unwarranted assumptions, but http://haskell.org/haskellwiki/Homework_help might be useful.
Second: div, mod, reverse, and the unfoldr function from Data.List will do what you want.
/g
On 11/25/06, escafia
wrote: Hi,
i've one fuction receiving an int . The objective is return the respective binary code of that int.
For example, if i receive 28 the fuction will return 011100.
So, if it is really homework, and looks like, here is the start
binary :: Int->[Int] binary 0 = [0] binary x = binary.... div and mod are enough for start :)

On Mon, Nov 27, 2006 at 06:20:15PM -0600, J. Garrett Morris wrote:
First, and forgive me if I'm making unwarranted assumptions, but http://haskell.org/haskellwiki/Homework_help might be useful.
On 11/25/06, escafia
wrote: i've one fuction receiving an int . The objective is return the respective binary code of that int.
For example, if i receive 28 the fuction will return 011100.
My question is that there is any fuction in libraries that do this? If not, anyone has any suggestion?
It depends on how you plan to represent bit strings. One of the best representations of bits strings for computation purposes is packed, rightmost bit at position 0, pad with 0's. If you choose that representation, the function you want is available in the Haskell '98 Prelude. It's called "id". I'd recommend using a newtype, however.

trevion:
Hello,
First, and forgive me if I'm making unwarranted assumptions, but http://haskell.org/haskellwiki/Homework_help might be useful.
Yes, almost certainly this is the case. The same questions were asked on
#haskell at pretty much the same time...
15:54:34 --- join: greg_lundien joined #haskell
15:54:51

escafia
Hi,
i've one fuction receiving an int . The objective is return the respective binary code of that int.
For example, if i receive 28 the fuction will return 011100.
In GHCi, Prelude> Numeric.showIntAtBase 2 (head . show) 28 $ "" "11100"
My question is that there is any fuction in libraries that do this? If not, anyone has any suggestion?
Other alternatives include, import Data.Bits import Data.List toBinary 0 = "0" toBinary i = reverse (unfoldr nextDigit i) where nextDigit 0 = Nothing nextDigit i = Just (head (show (i .&. 1)), shiftR i 1) or maybe, as you seem to want a certain number of digits, toBinary d i = concatMap show [ fromEnum (testBit i n) | n <- reverse [0..d-1] ] or likewise will work. It's probably best to just use showIntAtBase and pad it with however many 0's you want, though. -- Mark
participants (6)
-
dons@cse.unsw.edu.au
-
escafia
-
J. Garrett Morris
-
mark@ixod.org
-
Stefan O'Rear
-
Valentin Gjorgjioski