I have a question on how to create a diamond shape with paintChars. My code can be seen below. Here's a picture of how the shape should look
Sadly, I'm also not allowed to use Recursives
paintChars f size = putStrLn (genChars f size)
genChars :: ((Int, Int, Int) -> Char) -> Int -> [Char]
genChars f size =
paint size (map f [(x,y,size) | y <- [1..size],
x <- [1..size]])
where
paint 0 [] = []
paint 0 (c:cs) = '\n' : (paint size (c:cs))
paint n (c:cs) = c: (paint (n-1) cs)
diamond (x,y,size) =
if x>=y+20 || x<=y-20 then ' ' else '0'
main = paintChars diamond 40
I'm only able to create the right-diagonal lines, how can i code the left-diagonal?
Thanks for your help :)
I think you make things too complicated. For a diamond the first n/2 lines have a pattern where the i-th line contains (n-1)/2 - i + 1 spaces, and then 2×i+1
0
s:the rest is just mirroring this, so the
i
value here ranges from0
to20
and back:For example: