Created
October 31, 2015 09:44
-
-
Save hatashiro/e2114872b25527138a1f to your computer and use it in GitHub Desktop.
Sierpinski Triangle
This file contains hidden or 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
| module Main where | |
| import Data.List | |
| import Lib | |
| pad :: Int -> [Char] -> [Char] | |
| pad size str | |
| | length str < size = str ++ replicate (size - length str) ' ' | |
| | otherwise = str | |
| duplicate :: Int -> [[Char]] -> [[Char]] | |
| duplicate size triangle = | |
| let paddedTriangle = map (pad size) triangle | |
| in | |
| zipWith (++) paddedTriangle triangle | |
| getSierpinski :: Int -> [[Char]] | |
| getSierpinski 1 = ["*"] | |
| getSierpinski size = | |
| child ++ (duplicate childSize child) | |
| where | |
| childSize = size `div` 2 | |
| child = getSierpinski childSize | |
| drawSierpinski :: Int -> [Char] | |
| drawSierpinski 0 = "*\n" | |
| drawSierpinski size = | |
| (intercalate "\n" $ getSierpinski size) | |
| main :: IO () | |
| main = | |
| let sizes = take 8 $ iterate (*2) 1 | |
| in | |
| putStr $ foldl1 (++) $ map ((++ "\n\n") . drawSierpinski) sizes |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment