Skip to content

Instantly share code, notes, and snippets.

@janmasrovira
Last active February 20, 2018 21:59
Show Gist options
  • Save janmasrovira/11107ecb908f8492c06c18df4e001660 to your computer and use it in GitHub Desktop.
Save janmasrovira/11107ecb908f8492c06c18df4e001660 to your computer and use it in GitHub Desktop.
Ascii fractals
import System.Environment
type Pattern = [[Char]]
fractalize :: Pattern -> Int -> String
fractalize [] _ = ""
fractalize pat k = unlines [ [ charAt k i j | j <- [0..m^k - 1] ] | i <- [0..n^k - 1] ]
where
n = length pat
m = length (head pat)
charAt 1 i j = pat!!i!!j
charAt k i j
| x == '#' = pat!!(i`mod`n)!!(j`mod`m)
| otherwise = ' '
where x = charAt (k - 1) (i`div`n) (j`div`m)
pattern1 :: Pattern
pattern1 = [ " # "
, "###"
, " # "]
main :: IO ()
main = getK >>= putStr . fractalize pattern1
where getK = read . head <$> getArgs
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment