Show ASCII table: Difference between revisions

Content added Content deleted
m (→‎{{header|Haskell}}: Tidied. Used chunksOf from Data.List.Split)
Line 1,702: Line 1,702:
=={{header|Haskell}}==
=={{header|Haskell}}==
<lang haskell>import Data.List (intercalate, transpose)
<lang haskell>import Data.List (intercalate, transpose)
import Data.List.Split (chunksOf)
import Data.Char (chr)
import Data.Char (chr)

main :: IO ()
main = putStrLn asciiTable


asciiTable :: String
asciiTable :: String
Line 1,710: Line 1,714:
transpose (chunksOf 16 $ asciiEntry <$> [32 .. 127])
transpose (chunksOf 16 $ asciiEntry <$> [32 .. 127])


main :: IO ()
main = putStrLn asciiTable

-------------------------------------------------------------
asciiEntry :: Int -> String
asciiEntry :: Int -> String
asciiEntry n =
asciiEntry n
let k = asciiName n
| null k = k
| otherwise = concat [justifyRight 4 ' ' (show n), " : ", k]
in case k of
where
[] -> k
k = asciiName n
_ -> concat [justifyRight 4 ' ' (show n), " : ", k]


asciiName :: Int -> String
asciiName :: Int -> String
Line 1,728: Line 1,728:
| 127 == n = "Del"
| 127 == n = "Del"
| otherwise = [chr n]
| otherwise = [chr n]

chunksOf :: Int -> [a] -> [[a]]
chunksOf k = go
where
go t =
case splitAt k t of
(a, b)
| null a -> []
| otherwise -> a : go b


justifyLeft, justifyRight :: Int -> Char -> String -> String
justifyLeft, justifyRight :: Int -> Char -> String -> String
justifyLeft n c s = take n (s ++ replicate n c)
justifyLeft n c s = take n (s <> replicate n c)


justifyRight n c s = drop (length s) (replicate n c ++ s)</lang>
justifyRight n c = (drop . length) <*> (replicate n c <>)</lang>
{{Out}}
{{Out}}
<pre> 32 : Spc 48 : 0 64 : @ 80 : P 96 : ` 112 : p
<pre> 32 : Spc 48 : 0 64 : @ 80 : P 96 : ` 112 : p