-
-
Save Ino4137/e6e5c9597e51f45be949f4363c8dd5e2 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 = pascal n >>= (ascii . odd) | |
| where | |
| ascii True = "##" | |
| ascii False = " " | |
| sierpinskiTriangle :: Integer -> [String] | |
| sierpinskiTriangle n = map (centre . sierpinski) [0..n] | |
| where | |
| centre line = (spaces $ 1 + fromIntegral n - length line `div` 2) ++ line | |
| spaces = flip replicate ' ' | |
| 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