Skip to main content
3 of 3
added 110 characters in body

Rendering a cross in ASCII art

I've recently tried to write my functions by using composition. But this one, renderCross' is especially difficult to convert fully. How would I write this composition point free? It's the n that I'm trying to make disappear. I've tried using <*>, and pointfree.io uses liftA2 (but this results in very ugly code) so I would believe I'm at least somewhat close.

Maybe I could use the Reader monad, but would that be too complicated?

renderCross :: Int -> String
renderCross = spread . unlines . flip renderCross' [""]
  where
    renderCross' :: Int -> [String] -> [String]
    renderCross' n = edge n . vert n . horiz n . mid n . horiz n . vert n . edge n

edge :: Int -> [String] -> [String]
edge i s = s ++ [space i ++ "ooo"]

vert :: Int -> [String] -> [String]
vert i s = s ++ replicate (i - 2) (space i ++ "o|o")

horiz :: Int -> [String] -> [String]
horiz i s = s ++ [horiz' ++ "|" ++ horiz']
  where
    horiz' = replicate i 'o'

mid :: Int -> [String] -> [String]
mid i s = s ++ [intercalate (replicate (i - 1) '-') ["o", "o", "o"]]

space :: Int -> String
space n = replicate (n - 1) ' '

EDIT:

-- spread out word, i.e. add a space between each character
spread = intersperse ' '