Skip to content

Instantly share code, notes, and snippets.

@ailiev
Created November 7, 2011 23:44
Show Gist options
  • Save ailiev/1346573 to your computer and use it in GitHub Desktop.
Save ailiev/1346573 to your computer and use it in GitHub Desktop.
Haskell digit printing
> 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'
@ailiev
Copy link
Author

ailiev commented Nov 22, 2011

*Main> print_ascii [1,2,3,1,0]
   --  --     -- 
|    |   | | |  |
|  --  --  | |  |
| |      | | |  |
   --  --     -- 

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment