Haskell Generative ASCII Art

art haskell

Table of Contents

2D Grid

char :: Int -> Int -> Char
char x y
  | odd x = '#'
  | otherwise = ' '

grid :: Int -> Int -> (Int -> Int -> Char) -> String
grid w h f = unlines [[f x y | x <- [0..w-1]] | y <- [0..h-1]]

main :: IO ()
main = putStrLn $ grid 20 10 char
# # # # # # # # # #
# # # # # # # # # #
# # # # # # # # # #
# # # # # # # # # #
# # # # # # # # # #
# # # # # # # # # #
# # # # # # # # # #
# # # # # # # # # #
# # # # # # # # # #
# # # # # # # # # #

Sierpinski Triangle

import Data.Bits

char :: Int -> Int -> Char
char x y
  | (x .&. y) == 0 = '#'
  | otherwise = ' '

grid :: Int -> Int -> (Int -> Int -> Char) -> String
grid w h f = unlines [[f x y | x <- [0..w-1]] | y <- [0..h-1]]

main :: IO ()
main = putStrLn $ grid 32 32 char
################################
# # # # # # # # # # # # # # # #
##  ##  ##  ##  ##  ##  ##  ##
#   #   #   #   #   #   #   #
####    ####    ####    ####
# #     # #     # #     # #
##      ##      ##      ##
#       #       #       #
########        ########
# # # #         # # # #
##  ##          ##  ##
#   #           #   #
####            ####
# #             # #
##              ##
#               #
################
# # # # # # # #
##  ##  ##  ##
#   #   #   #
####    ####
# #     # #
##      ##
#       #
########
# # # #
##  ##
#   #
####
# #
##
#

Rectangles

import Data.Bits

rect :: Int -> Int -> Int -> Int -> Char
rect w h x y
  | (x < w && y < h) = '#'
  | otherwise = '_'

grid :: Int -> Int -> (Int -> Int -> Char) -> String
grid w h f = unlines [[f x y | x <- [0..w-1]] | y <- [0..h-1]]

main :: IO ()
main = putStrLn $ grid 32 16 $ rect 16 16
################________________
################________________
################________________
################________________
################________________
################________________
################________________
################________________
################________________
################________________
################________________
################________________
################________________
################________________
################________________
################________________

Translation

import Data.Bits

translate :: Int -> Int -> (Int -> Int -> Char) -> Int -> Int -> Char
translate tx ty f x y = f (x - tx) (y - ty)

rect :: Int -> Int -> Int -> Int -> Char
rect w h x y
  | (0 <= x && x < w && 0 <= y && y < h) = '#'
  | otherwise = '_'

grid :: Int -> Int -> (Int -> Int -> Char) -> String
grid w h f = unlines [[f x y | x <- [0..w-1]] | y <- [0..h-1]]

main :: IO ()
main = putStrLn $ grid 16 16 $ translate 4 4 $ rect 8 8
________________
________________
________________
________________
____########____
____########____
____########____
____########____
____########____
____########____
____########____
____########____
________________
________________
________________
________________

Repetition

import Data.Bits

translate :: Int -> Int -> (Int -> Int -> Char) -> Int -> Int -> Char
translate tx ty f x y = f (x - tx) (y - ty)

repeat' :: Int -> Int -> (Int -> Int -> Char) -> Int -> Int -> Char
repeat' rx ry f x y = f (x `mod` rx) (y `mod` ry)

rect :: Int -> Int -> Int -> Int -> Char
rect w h x y
  | (0 <= x && x < w && 0 <= y && y < h) = '#'
  | otherwise = '_'

grid :: Int -> Int -> (Int -> Int -> Char) -> String
grid w h f = unlines [[f x y | x <- [0..w-1]] | y <- [0..h-1]]

main :: IO ()
main = putStrLn $ grid 32 16 $ repeat' 8 8 $ rect 4 4
####____####____####____####____
####____####____####____####____
####____####____####____####____
####____####____####____####____
________________________________
________________________________
________________________________
________________________________
####____####____####____####____
####____####____####____####____
####____####____####____####____
####____####____####____####____
________________________________
________________________________
________________________________
________________________________

Stacking

import Data.Bits
import Data.Maybe

translate :: Int -> Int -> (Int -> Int -> Maybe Char) -> Int -> Int -> Maybe Char
translate tx ty f x y = f (x - tx) (y - ty)

repeat' :: Int -> Int -> (Int -> Int -> Maybe Char) -> Int -> Int -> Maybe Char
repeat' rx ry f x y = f (x `mod` rx) (y `mod` ry)

rect :: Char -> Int -> Int -> Int -> Int -> Maybe Char
rect c w h x y
  | (0 <= x && x < w && 0 <= y && y < h) = Just c
  | otherwise = Nothing

fill :: Char -> (Int -> Int -> Maybe Char)
fill c = \_ _ -> Just c

grid :: Int -> Int -> (Int -> Int -> Maybe Char) -> String
grid w h f = unlines [[fromMaybe ' ' $ f x y | x <- [0..w-1]] | y <- [0..h-1]]

(<|>) :: (Int -> Int -> Maybe Char) -> (Int -> Int -> Maybe Char) -> Int -> Int -> Maybe Char
(<|>) f g x y = case (f x y) of
  Just c -> Just c
  Nothing -> g x y

rects :: Char -> (Int -> Int -> Maybe Char)
rects c = repeat' 8 8 $ rect c 4 4

main :: IO ()
main = putStrLn $ grid 32 16 $
  (rects '#')
  <|> translate 1 1 (rects '.')
  <|> translate 2 2 (rects '#')
  <|> fill ' '
####    ####    ####    ####
####.   ####.   ####.   ####.
####.#  ####.#  ####.#  ####.#
####.#  ####.#  ####.#  ####.#
 ....#   ....#   ....#   ....#
  ####    ####    ####    ####


####    ####    ####    ####
####.   ####.   ####.   ####.
####.#  ####.#  ####.#  ####.#
####.#  ####.#  ####.#  ####.#
 ....#   ....#   ....#   ....#
  ####    ####    ####    ####




If you have an idea how this page could be improved or a comment send me a mail.