Last active
December 23, 2022 16:57
-
-
Save Martinsos/e7a0f771a672586a862d6c03e77b0555 to your computer and use it in GitHub Desktop.
Short Haskell function that prints a christmas tree
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
printTree :: Int -> IO () | |
printTree h = (putStrLn . unlines . center) $ "()" : (showTreeLvl <$> [0 .. h]) ++ ["[]"] | |
where showTreeLvl l = "/" ++ (take (l * 2) . drop (l `mod` 3) . concat . repeat) "~~o~~*~o~~*~~o~*" ++ "\\" | |
center ss = let w = maximum (length <$> ss) in (\s -> concat (replicate ((w - length s) `div` 2) " ") ++ s) <$> ss | |
-- > printTree 5 | |
-- | |
-- () | |
-- /\ | |
-- /~o\ | |
-- /o~~*\ | |
-- /~~o~~*\ | |
-- /~o~~*~o~\ | |
-- /o~~*~o~~*~\ | |
-- [] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment