# Haskell Program to Print Half Diamond Star Pattern

We can create a half-diamond star pattern in Haskell using recursive and replicate functions. The half-diamond star pattern is a pattern made up of asterisks (*) arranged in the shape of a half-diamond. It is typically created by printing a series of asterisks in a pyramid shape, starting with a single asterisk in the first row, two asterisks in the second row, and so on, until the middle row which contains the maximum number of asterisks. From that row onwards, the number of asterisks decreases until there is only a single asterisk in the last row.

## Algorithm

• Step 1 − The printRow function is defined using replicate/recursive function

• Step 2 − Program execution will be started from the main function. The main() function has whole control of the program. It is written as main = do. In the main function, a number is passed up to which the half-diamond star pattern is to be printed.

• Step 3 − The variable named, “n” is initialized. It will hold the integer up to which the half-diamond star pattern is to be printed.

• Step 4 − The result is printed to the console using the ‘putStrLn’ statement after the function is called.

## Example 1

In this example, printRow is a recursive function that takes two arguments n and i representing the size of the pattern and the current row, respectively. If i is equal to 0, it returns a newline character. If i is less than or equal to n div 2, it returns a string with spaces equal to n div 2 - i followed by a single asterisk, followed by a newline character, followed by a recursive call to printRow with i - 1 as the updated row number. Otherwise, it returns a string with spaces equal to i - n div 2 followed by a single asterisk, followed by a newline character, followed by a recursive call to printRow with i - 1 as the updated row number.

module Main where

printRow :: Int -> Int -> String
printRow n i
| i == 0    = ""
| i <= n div 2 = " " ++ replicate (n div 2 - i) ' ' ++ "*" ++ "" ++ printRow n (i - 1)
| otherwise = " " ++ replicate (i - n div 2) ' ' ++ "*" ++ "" ++ printRow n (i - 1)

printPattern :: Int -> String
printPattern n = printRow n (n - 1)

main :: IO ()
main = putStr (printPattern 8)


## Output

   *
*
*
*
*
*
*


## Example 2

In this example, the printRow function takes two arguments n and i representing the size of the pattern and the current row, respectively. It returns a string with spaces equal to n div2 - abs (ndiv 2 - i) followed by a single asterisk and a newline character.

The printPattern function takes an integer n as an argument and returns a string created by concatenating the result of printRow for each row in the range [0..n - 1].

module Main where

printRow :: Int -> Int -> String
printRow n i = replicate (n div 2 - abs (n div 2 - i)) ' ' ++ "*"

printPattern :: Int -> String
printPattern n = concat [ printRow n i | i <- [0..n - 1]]

main :: IO ()
main = putStr (printPattern 8)


## Output

*
*
*
*
*
*
*
*


## Conclusion

In Haskell, to print the half diamond star pattern we can use replicate, div and recursive functions.

Updated on: 06-Apr-2023

47 Views 