tasks a-s
This commit is contained in:
parent
47bf37c096
commit
b83f433714
12433 changed files with 156208 additions and 123 deletions
16
Task/Binary-digits/Haskell/binary-digits.hs
Normal file
16
Task/Binary-digits/Haskell/binary-digits.hs
Normal file
|
|
@ -0,0 +1,16 @@
|
|||
import Data.List
|
||||
import Numeric
|
||||
import Text.Printf
|
||||
|
||||
-- Use the built-in function showIntAtBase.
|
||||
toBin n = showIntAtBase 2 ("01" !!) n ""
|
||||
|
||||
-- Implement our own version.
|
||||
toBin' 0 = []
|
||||
toBin' x = (toBin' $ x `div` 2) ++ (show $ x `mod` 2)
|
||||
|
||||
printToBin n = putStrLn $ printf "%4d %14s %14s" n (toBin n) (toBin' n)
|
||||
|
||||
main = do
|
||||
putStrLn $ printf "%4s %14s %14s" "N" "toBin" "toBin'"
|
||||
mapM_ printToBin [5, 50, 9000]
|
||||
Loading…
Add table
Add a link
Reference in a new issue