You are viewing a single comment's thread. Return to all comments →
haskell - there is a function for the normal triangle when we hit 0 and one for the fractal one
triangle:: Int-> Int-> [String] triangle width height = let halfWidth = (div (width - 1) 2) in [(replicate (halfWidth-x) '_') ++ (replicate x '1') ++ "1" ++ (replicate x '1') ++ (replicate (halfWidth - x) '_') | x <- [0..halfWidth]] sierpinskiTriangle:: Int-> Int-> Int-> [String] sierpinskiTriangle 0 width height = (triangle width height) sierpinskiTriangle n width height = let newWidth = (div (width - 1) 2) newHeight = (div height 2) smallerTriangle = (sierpinskiTriangle (n-1) newWidth newHeight) padding = (replicate ((div newWidth 2) + 1) '_') in (map (\x -> padding ++ x ++ padding) smallerTriangle) ++ (map (\x -> x ++ "_" ++ x) smallerTriangle) main = do nString <- getLine let n = (read nString :: Int) putStrLn (foldl (\x y -> x ++ y ++ "\n") "" (sierpinskiTriangle n 63 32))
Seems like cookies are disabled on this browser, please enable them to open this website
Functions and Fractals: Sierpinski triangles
You are viewing a single comment's thread. Return to all comments →
haskell - there is a function for the normal triangle when we hit 0 and one for the fractal one