How do I create a diamond shape in haskell? paintChars

356 Views Asked by At

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

enter image description here

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 :)

1

There are 1 best solutions below

3
On

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 0s:

line :: Int -> Int -> String
line n i = replicate (div (n-1) 2 - i + 1) ' ' ++ replicate (2*i+1) '0'

the rest is just mirroring this, so the i value here ranges from 0 to 20 and back:

printDiamond :: Int -> IO ()
printDiamond n = mapM_ (putStrLn . line n) (r ++ (n2 : reverse r))
    where n2 = div n 2
          r = [0 .. n2-1]

For example:

Prelude> printDiamond 4
  0
 000
00000
 000
  0
Prelude> printDiamond 6
   0
  000
 00000
0000000
 00000
  000
   0
Prelude> printDiamond 8
    0
   000
  00000
 0000000
000000000
 0000000
  00000
   000
    0
Prelude> printDiamond 10
     0
    000
   00000
  0000000
 000000000
00000000000
 000000000
  0000000
   00000
    000
     0
Prelude> printDiamond 20
          0
         000
        00000
       0000000
      000000000
     00000000000
    0000000000000
   000000000000000
  00000000000000000
 0000000000000000000
000000000000000000000
 0000000000000000000
  00000000000000000
   000000000000000
    0000000000000
     00000000000
      000000000
       0000000
        00000
         000
          0
Prelude> printDiamond 40
                    0
                   000
                  00000
                 0000000
                000000000
               00000000000
              0000000000000
             000000000000000
            00000000000000000
           0000000000000000000
          000000000000000000000
         00000000000000000000000
        0000000000000000000000000
       000000000000000000000000000
      00000000000000000000000000000
     0000000000000000000000000000000
    000000000000000000000000000000000
   00000000000000000000000000000000000
  0000000000000000000000000000000000000
 000000000000000000000000000000000000000
00000000000000000000000000000000000000000
 000000000000000000000000000000000000000
  0000000000000000000000000000000000000
   00000000000000000000000000000000000
    000000000000000000000000000000000
     0000000000000000000000000000000
      00000000000000000000000000000
       000000000000000000000000000
        0000000000000000000000000
         00000000000000000000000
          000000000000000000000
           0000000000000000000
            00000000000000000
             000000000000000
              0000000000000
               00000000000
                000000000
                 0000000
                  00000
                   000
                    0