Haskell Program to Print Pyramid Star Pattern


In Haskell, we can use mapM and ForM functions to print pyramid-patterned stars. A pyramid star pattern is a design or arrangement of stars or other symbols in the shape of a pyramid. It is created by printing stars or symbols in multiple rows, starting from the top and moving downwards. Each row contains one more symbol than the previous row, creating the illusion of a triangular shape. The pattern is usually symmetrical, with the number of stars or symbols in each row equal to the row number, and the middle of each row lined up vertically.

Algorithm

  • Step 1 − The pyramid function is defined using mapM_ function

  • Step 2 − Program execution will be started from 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 pyramid star pattern is to be printed.

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

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

In this example,a function pyramid is defined that takes an integer n as an argument, and uses mapM_ to print each element of the list generated by the buildPyramid function. The buildPyramid function takes n as an argument and generates a list of strings that represent the pyramid pattern. It uses a list comprehension to generate a string for each row of the pyramid, by concatenating two strings.

Example 1

In this example, the function pyramid is defined using the mapM_ function to print the pyramid star pattern.

pyramid :: Int -> IO ()
pyramid n = mapM_ putStrLn $ buildPyramid n
   where
      buildPyramid :: Int -> [String]
      buildPyramid n = [replicate (n-i) ' ' ++ replicate (2*i-1) '*' | i <- [1..n]]

main :: IO ()
main = do
   let n = 5
   pyramid n

Output

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

Example 2

In this example, a function pyramid is defined that takes an integer n as an argument, and uses the forM_ function to iterate over the range [1..n]. The function passed to forM_ is an anonymous function (also known as a lambda function) that takes a single argument i, and uses putStrLn to print the string generated by concatenating two strings.

pyramid :: Int -> IO ()
pyramid n = mapM_ putStrLn $ buildPyramid n
   where
      buildPyramid :: Int -> [String]
      buildPyramid n = [replicate (n-i) ' ' ++ replicate (2*i-1) '*' | i <- [1..n]]

main :: IO ()
main = do
   let n = 5
   pyramid n

Output

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

Example 3

In this method, a printTriangle function is defined that takes two arguments: an integer n representing the number of rows and an integer i representing the current row. The function checks if i is greater than n, and if it is, the function returns without doing anything. If i is not greater than n, the function prints a line of i asterisks using the putStrLn function, and then calls itself with i incremented by 1. This recursive approach continues until i is greater than n, at which point the function returns.

drawLine :: Int -> Int -> IO ()
drawLine n k = do
   let spaces = replicate (n - k) ' '
   let stars = replicate (2 * k - 1) '*'
   putStrLn (spaces ++ stars)

drawPyramid :: Int -> IO ()
drawPyramid n = mapM_ (drawLine n) [1..n]

main :: IO ()
main = do
   let n = 5
   drawPyramid n

Output

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

Conclusion

In Haskell, to print the pyramid star pattern we can use MapM_ or forM_ functions or we can use recursive function.

Updated on: 20-Apr-2023

130 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements