Last active
April 25, 2018 13:29
-
-
Save Xophmeister/6902377 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
Good job, although you might want to take a look at my fork as I've cleaned up your code a bit and also improved the performance.
Notable changes:
concat $ map ...is a pattern that can and should be replaced with>>=as it does exacly that on lists[0] ++ ...into0 : ...serpinskiTrianglefunction. You should avoid nesting where clauses.I'm aware that this is a 3 year old gist, but it's fairly well positioned in google so it was worth to take the time and polish it.