Binary digits: Difference between revisions
Content added Content deleted
Not a robot (talk | contribs) (Add False) |
m (→{{header|Haskell}}: Added a variation) |
||
Line 2,320: | Line 2,320: | ||
9000 10001100101000 10001100101000 |
9000 10001100101000 10001100101000 |
||
</pre> |
</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}}== |
=={{header|Icon}} and {{header|Unicon}}== |