-
-
Save swr1bm86/d82e20c74127bd2b403e to your computer and use it in GitHub Desktop.
Sierpinski Triangle in Haskell
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
| sumPairs :: [Integer] -> [Integer] | |
| sumPairs (x:y:s) = (x + y) : sumPairs (y:s) | |
| sumPairs _ = [] | |
| pascal :: Integer -> [Integer] | |
| pascal 0 = [1] | |
| pascal n = sumPairs $ [0] ++ (pascal $ n - 1) ++ [0] | |
| sierpinski :: Integer -> String | |
| sierpinski n = concat $ map (ascii . odd) $ pascal n | |
| where ascii True = "##" | |
| ascii False = " " | |
| sierpinskiTriangle :: Integer -> [String] | |
| sierpinskiTriangle n = map (centre . sierpinski) [0..n] | |
| where centre line = (spaces padding) ++ line | |
| where padding = 1 + (fromIntegral n) - ((length line) `div` 2) | |
| spaces m = take m [' ',' '..] | |
| main :: IO () | |
| main = putStr $ unlines $ sierpinskiTriangle 63 |
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
| ## | |
| #### | |
| ## ## | |
| ######## | |
| ## ## | |
| #### #### | |
| ## ## ## ## | |
| ################ | |
| ## ## | |
| #### #### | |
| ## ## ## ## | |
| ######## ######## | |
| ## ## ## ## | |
| #### #### #### #### | |
| ## ## ## ## ## ## ## ## | |
| ################################ | |
| ## ## | |
| #### #### | |
| ## ## ## ## | |
| ######## ######## | |
| ## ## ## ## | |
| #### #### #### #### | |
| ## ## ## ## ## ## ## ## | |
| ################ ################ | |
| ## ## ## ## | |
| #### #### #### #### | |
| ## ## ## ## ## ## ## ## | |
| ######## ######## ######## ######## | |
| ## ## ## ## ## ## ## ## | |
| #### #### #### #### #### #### #### #### | |
| ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## | |
| ################################################################ | |
| ## ## | |
| #### #### | |
| ## ## ## ## | |
| ######## ######## | |
| ## ## ## ## | |
| #### #### #### #### | |
| ## ## ## ## ## ## ## ## | |
| ################ ################ | |
| ## ## ## ## | |
| #### #### #### #### | |
| ## ## ## ## ## ## ## ## | |
| ######## ######## ######## ######## | |
| ## ## ## ## ## ## ## ## | |
| #### #### #### #### #### #### #### #### | |
| ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## | |
| ################################ ################################ | |
| ## ## ## ## | |
| #### #### #### #### | |
| ## ## ## ## ## ## ## ## | |
| ######## ######## ######## ######## | |
| ## ## ## ## ## ## ## ## | |
| #### #### #### #### #### #### #### #### | |
| ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## | |
| ################ ################ ################ ################ | |
| ## ## ## ## ## ## ## ## | |
| #### #### #### #### #### #### #### #### | |
| ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## | |
| ######## ######## ######## ######## ######## ######## ######## ######## | |
| ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## | |
| #### #### #### #### #### #### #### #### #### #### #### #### #### #### #### #### | |
| ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## | |
| ################################################################################################################################ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment