Haskell Program to Print Reverse Pyramid Star Pattern


Haskell has internal functions like mapM and ForM functions to print a revered Pyramid pattern star.

A reverse pyramid star pattern is a pattern of asterisks (stars) arranged in the shape of an inverted pyramid.

Algorithm

  • Step 1 − 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 reverse pyramid star pattern is to be printed. The main function defines the mapM_ function as,

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

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

Example 1

In this example, a main function is defined that uses the mapM_ function to apply the action putStrLn to each element in the list [replicate i ' ' ++ replicateicate (2 * (n - i) - 1) '*' | i <- [0..n - 1]]. The replicate function is used to create strings of spaces and stars, and the ++ operator is used to concatenate the strings.

module Main where

main :: IO ()
main = do
   let n = 5
   mapM_ putStrLn [replicate i ' ' ++ replicate (2 * (n - i) - 1) '*' | i <- [0..n - 1]]

Output

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

Example 2

In this method, a main function is defined that uses the forM_ function from the Control.Monad module to iterate over the list [n, n-1..1]. The function passed to forM_ takes an integer i as an argument, and creates a string by concatenating the result of two calls to the replicate function. The first call to replicate creates a string of n - i spaces, while the second call creates a string of 2 * i - 1 stars. The resulting string is then printed to the console using the putStrLn function.

module Main where

import Control.Monad

main :: IO ()
main = do
   let n = 5
   forM_ [n, n-1..1] $ \i -> do
      putStrLn $ replicate (n - i) ' ' ++ replicate (2 * i - 1) '*'

Output

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

Example 3

In this example, a function printStars is defined that takes two arguments, n and i. If i is equal to 0, the function returns (). Otherwise, the function first creates a string of spaces and asterisks by concatenating the result of two calls to the replicate function, then prints the string to the console using putStrLn. Finally, the function calls itself recursively with i decremented by 1, until i is equal to 0.

module Main where

printStars :: Int -> Int -> IO ()
printStars n i
   | i == 0    = return ()
   | otherwise = do
      putStrLn $ replicate (n - i) ' ' ++ replicate (2 * i - 1) '*'
      printStars n (i - 1)

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

Output

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

Conclusion

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

Updated on: 20-Apr-2023

114 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements