Functions and Fractals: Sierpinski triangles

  • + 0 comments

    I seemed to have thought about it differently. I saw each triangle a square where each coordinate is colored 1 if (c<=r) and 0 if (c>r). If you add a modulo function over powers of two, you can then generate each succesively smaller pattern. If you multiply them all together you end up with a single function that you then can apply over the range of rows and columns, drop the odd rows, pretty this up into a string and then print it out.

    haskell:

    sm :: Int -> Int -> Int -> Int
    sm r c m
    | (mod c m) > (mod r m) = 0
    | otherwise = 1
    
    mv :: [Int] -> [Int] -> [Int]
    mv [] [] = []
    mv (x:xs) (y:ys) = (x*y):(mv xs ys)
    
    prod :: Int -> Int -> Int -> [Int]
    prod d r 1 = [sm r c d | c<-[0..(d-1)]]
    prod d r l = mv ([sm r c (div d (2^(l-1))) | c<-[0..(d-1)]]) (prod d r (l-1))
    
    sier :: Int -> Int -> [[Int]]
    sier d l = [prod d r l | r <- [0,2..(d-1)]]
    
    
    sier :: Int -> Int -> [[Int]]
    sier d l = [prod d r l | r <- [0,2..(d-1)]]
     
    

    `