Skip to content

Instantly share code, notes, and snippets.

@Xophmeister
Last active April 25, 2018 13:29
Show Gist options
  • Save Xophmeister/6902377 to your computer and use it in GitHub Desktop.
Save Xophmeister/6902377 to your computer and use it in GitHub Desktop.
Sierpinski Triangle in Haskell
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
##
####
## ##
########
## ##
#### ####
## ## ## ##
################
## ##
#### ####
## ## ## ##
######## ########
## ## ## ##
#### #### #### ####
## ## ## ## ## ## ## ##
################################
## ##
#### ####
## ## ## ##
######## ########
## ## ## ##
#### #### #### ####
## ## ## ## ## ## ## ##
################ ################
## ## ## ##
#### #### #### ####
## ## ## ## ## ## ## ##
######## ######## ######## ########
## ## ## ## ## ## ## ##
#### #### #### #### #### #### #### ####
## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ##
################################################################
## ##
#### ####
## ## ## ##
######## ########
## ## ## ##
#### #### #### ####
## ## ## ## ## ## ## ##
################ ################
## ## ## ##
#### #### #### ####
## ## ## ## ## ## ## ##
######## ######## ######## ########
## ## ## ## ## ## ## ##
#### #### #### #### #### #### #### ####
## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ##
################################ ################################
## ## ## ##
#### #### #### ####
## ## ## ## ## ## ## ##
######## ######## ######## ########
## ## ## ## ## ## ## ##
#### #### #### #### #### #### #### ####
## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ##
################ ################ ################ ################
## ## ## ## ## ## ## ##
#### #### #### #### #### #### #### ####
## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ##
######## ######## ######## ######## ######## ######## ######## ########
## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ##
#### #### #### #### #### #### #### #### #### #### #### #### #### #### #### ####
## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ##
################################################################################################################################
@Ino4137
Copy link

Ino4137 commented Apr 25, 2018

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] ++ ... into 0 : ...
  • cleaned up the serpinskiTriangle function. 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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment