Created
November 7, 2011 23:44
-
-
Save ailiev/1346573 to your computer and use it in GitHub Desktop.
Haskell digit printing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
> import List (transpose, intersperse) | |
> import Char (isSpace) | |
> ref::[String] | |
> ref = [ | |
> " -- -- -- -- -- -- -- -- ", | |
> "| | | | | | | | | | | | | |", | |
> "| | | -- -- -- -- |-- | |--| -- ", | |
> "| | | | | | | | | | | | |", | |
> " -- -- -- -- -- -- -- " | |
> ] | |
The digits, each in column-major order, for easy assembly | |
> ref_digits :: [[String]] | |
> ref_digits = splits (all isSpace) ref_rows | |
> where ref_rows = transpose ref | |
Assemble the result column by column and then transpose ready for printing | |
> ascii_digit_rows :: [Int] -> [String] | |
> ascii_digit_rows = transpose . concat . intersperse spacer . map (ref_digits !!) | |
> where spacer = [(take 5 $ repeat ' ')] | |
> print_ascii :: [Int] -> IO() | |
> print_ascii = mapM_ putStrLn . ascii_digit_rows | |
generalization of List.words: split a list into the pieces separated by a delimiter | |
defined by a boolean function. | |
> splits :: (a -> Bool) -> [a] -> [[a]] | |
> splits p xs = case dropWhile p xs of | |
> [] -> [] | |
> s' -> prefix : splits p rest | |
> where (prefix, rest) = break p s' |
Author
ailiev
commented
Nov 22, 2011
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment