Skip to content

Instantly share code, notes, and snippets.

@Martinsos
Last active December 23, 2022 16:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Martinsos/e7a0f771a672586a862d6c03e77b0555 to your computer and use it in GitHub Desktop.
Save Martinsos/e7a0f771a672586a862d6c03e77b0555 to your computer and use it in GitHub Desktop.
Short Haskell function that prints a christmas tree
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