Binary digits: Difference between revisions
m
→{{header|Haskell}}: Added a variation
Not a robot (talk | contribs) (Add False) |
m (→{{header|Haskell}}: Added a variation) |
||
Line 2,320:
9000 10001100101000 10001100101000
</pre>
and in terms of first and swap, we could also write this as:
<lang haskell>import Data.Bifunctor (first)
import Data.List (unfoldr)
import Data.Tuple (swap)
---------------------- BINARY DIGITS ---------------------
binaryDigits :: Int -> String
binaryDigits = reverse . unfoldr go
where
go 0 = Nothing
go n = Just . first ("01" !!) . swap . quotRem n $ 2
--------------------------- TEST -------------------------
main :: IO ()
main =
mapM_
( putStrLn
. ( ((<>) . (<> " -> ") . show)
<*> binaryDigits
)
)
[5, 50, 9000]</lang>
{{Out}}
<pre>5 -> 101
50 -> 110010
9000 -> 10001100101000</pre>
=={{header|Icon}} and {{header|Unicon}}==
|