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 <escafia@gmail.com> 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.
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 <escafia@gmail.com> 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 <escafia@gmail.com> 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 <greg_lundien> is there a library function in haskell to convert from decimal to binary?? 15:55:19 <sjanssen> @hoogle showIntAtBase 15:55:20 <lambdabot> Numeric.showIntAtBase :: Integral a => a -> (Int -> Char) -> a -> ShowS 15:55:39 <greg_lundien> sjanssen: thanks 15:56:10 <sjanssen> > showIntAtBase 100 (head . show) 2 "" 15:56:11 <lambdabot> "2" 15:56:30 <sjanssen> > showIntAtBase 2 (head . show) 100 "" 15:56:31 <lambdabot> "1100100" 15:57:04 <dons> greg_lundien: why do you need to convert to binary? 15:57:23 <kpreid> > showIntAtBase 2 Char.intToDigit 100 "" 15:57:24 <lambdabot> "1100100" 15:57:42 <greg_lundien> dons: uh... long story? do you know how to do it? 15:58:29 <vincenz> all ints are binary 15:58:37 <greg_lundien> the showIntAtBase thing doesn't work with guys 15:58:45 <vincenz> only with girls? 15:58:47 <greg_lundien> *with hugs, not guys 15:58:50 <greg_lundien> lolz 15:58:54 <dons> :m + Numeric 15:58:56 <Cale> import Numeric 15:58:59 <greg_lundien> oic 15:59:01 <dons> ?hoogle showIntAtBase 15:59:02 <lambdabot> Numeric.showIntAtBase :: Integral a => a -> (Int -> Char) -> a -> ShowS 15:59:36 <greg_lundien> cant import numeric 15:59:44 --- quit: greg_lundien ("Leaving") -- Don
escafia <escafia@gmail.com> writes:
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