Skip to content

Instantly share code, notes, and snippets.

@hatashiro
Created October 31, 2015 09:44
Show Gist options
  • Select an option

  • Save hatashiro/e2114872b25527138a1f to your computer and use it in GitHub Desktop.

Select an option

Save hatashiro/e2114872b25527138a1f to your computer and use it in GitHub Desktop.
Sierpinski Triangle
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