# Haskell Program to Print Diamond Star Pattern

In Haskell, to print Diamond star pattern we will be using mapM_, reverse, unlines and replicate functions. In the first example, we are going to use ( diamondStarPattern n = unlines $topHalf ++ bottomHalf where topHalf = [replicate (n - i) ' ' ++ replicate ((2 * i) - 1) '*' | i <- [0..n]] and bottomHalf = reverse$ drop 0 topHalf) function and in the second example, we are going to use, (diamondStarPattern n = mapM_ putStrLn $bottomRows ++ topRows where topRows = [spaces i ++ stars (n - 2 * i) | i <- [0..n div 2]] and bottomRows = reverse [spaces i ++ stars (n - 2 * i) | i <- [1..n div 2]]) function. ## Algorithm • Step 1 − The diamondStarPattern function is defined using mapM_ and reverse functions • Step 2 − The program execution will be started from main function. The main() function has whole control of the program. • Step 3 − The resultant diamond star pattern is printed to the console, once the function is being called with argument. ## Example 1 In this example, the diamond star pattern is printed using unlines and replicate function. main = do let n = 3 putStrLn$ diamondStarPattern n

diamondStarPattern :: Int -> String
diamondStarPattern n = unlines $topHalf ++ bottomHalf where topHalf = [replicate (n - i) ' ' ++ replicate ((2 * i) - 1) '*' | i <- [0..n]] bottomHalf = reverse$ drop 0 topHalf


## Output

  [1 of 1] Compiling Main             ( main.hs, main.o )

*
***
*****
*****
***
*


## Example 2

In this example, the diamond star pattern is printed using mapM_ and reverse function.

diamondStarPattern :: Int -> IO ()
diamondStarPattern n = mapM_ putStrLn \$  bottomRows ++ topRows
where
topRows = [spaces i ++ stars (n - 2 * i) | i <- [0..n div 2]]
bottomRows = reverse [spaces i ++ stars (n - 2 * i) | i <- [1..n div 2]]
stars x = replicate x '*'
spaces x = replicate x ' '

main = diamondStarPattern 7


## Output

  [1 of 1] Compiling Main             ( main.hs, main.o )
*
***
*****
*******
*****
***
*


## Conclusion

The diamond star pattern is a pattern made up of asterisks (*) arranged in the shape of a diamond. The pattern consists of two parts - an upper triangle and a lower triangle. The upper triangle is formed by a series of lines that gradually decrease in length from the center until they meet at a point at the top. The lower triangle is formed by a series of lines that gradually increase in length from the center until they reach the maximum width at the bottom. The two triangles are separated by a single line of asterisks. The overall effect is a diamond shape with a star pattern.