Haskell Program to Print Inverted Star Pattern


In Haskell, to print Inverted star pattern we will be using mapM_, reverse, unlines and replicate functions. In the first example, we are going to use ( invertedStarPattern n = mapM_ putStrLn (reverse [replicate i '*' | i <- [1..n]])) function and in the second example, we are going to use, invertedTriangle n = unlines $ reverse [replicate i '*' | i <- [1..n]]) function. In the third example, we are going to use (mapM_ putStrLn $ map (concat . flip replicate "*") rows) function.

Method 1: Printing Inverted Star Pattern using mapM_, reverse and replicate function .

In this method, the pattern is generated using a list comprehension that creates a list of strings, where each string is a row of the inverted star pattern. The replicate function is used to create a string of '*' characters of length i for each i in the range n to 1.

Algorithm

  • Step 1 − The invertedStarPattern is defined using mapM_, reverse and replicate function as, invertedStarPattern n = mapM_ putStrLn (reverse [replicate i '*' | i <- [1..n]]).

  • Step 2 − The program execution will be started from main function. The main() function has whole control of the program. It is written as main = do.

  • Step 3 − The variable named, ‘n’ is being initialized that will hold the value of number of rows up to which the inverted star pattern is to be printed.

  • Step 4 − The resultant inverted star pattern is printed to the console, once the function is being called.

Example

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

invertedStarPattern :: Int -> IO ()
invertedStarPattern n = mapM_ putStrLn (reverse [replicate i '*' | i <- [1..n]])

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

Output

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

Method 2: Printing InvertedTriangle Star Pattern using unlines function

In this method, the invertedTriangle function takes an integer n as an input and returns a string that represents the inverted triangle star pattern. The replicate i '*' expression creates a string consisting of i asterisks. The list comprehension [replicate i '*' | i <- [1..n]] generates a list of strings where each string is a sequence of asterisks. The length of each string increases from 1 to n. The reverse function is applied to the list generated in step 4 to reverse the order of the elements in the list. The unlines function is then applied to the list generated in step 5 to join the elements of the list into a single string separated by newline characters.

Algorithm

  • Step 1 − The program execution will be started from main function. The main() function has whole control of the program. It is written as main = do.

  • Step 2 − The variable named, ‘n’ is being initialized that will hold the value of number of rows up to which the inverted triangle star pattern is to be printed.

  • Step 3 − The invertedTriangle function is defined using unlines and reverse function as, invertedTriangle n = unlines $ reverse [replicate i '*' | i <- [1..n]].

  • Step 4 − The resultant inverted star pattern is printed to the console, once the function is being called.

Example

In this example, the invertedTriangle takes an Int as its argument and returns an IO action that prints out the inverted triangle pattern. The pattern is generated using a list comprehension that creates a list of strings with the appropriate number of asterisks for each row of the triangle. The mapM_ function then prints each string in the list on a new line.

main = do
  let n = 5
  putStrLn $ invertedTriangle n

invertedTriangle :: Int -> String
invertedTriangle n = unlines $ reverse [replicate i '*' | i <- [1..n]]

Output

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

Method 3: Printing Inverted Triangle Star Pattern using flip replicate function

In this method, a list of integers ‘rows’ is defined that counts down from n to 1, and then uses a combination of ‘map’ and ‘putStrLn’ to print a string of asterisks for each integer in the list.

Algorithm

  • Step 1 − The program execution will be started from main function. The main() function has whole control of the program. It is written as main = do.

  • Step 2 − The variable named, ‘n’ is being initialized that will hold the value of number of rows up to which the inverted triangle star pattern is to be printed.

  • Step 3 − The mapM_ function is used to print the resultant inverted triangle star pattern along with the flip replicate function as, mapM_ putStrLn $ map (concat . flip replicate "*") rows.

Example

In this example, the inverted triangle star pattern is printed using mapM_ and flip replicate function defined under main function.

main :: IO ()
main = do
  let n = 5
  let rows = reverse [1..n]
  mapM_ putStrLn $ map (concat . flip replicate "*") rows

Output

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

Conclusion

An inverted star triangle pattern is a pattern of asterisks (*) printed in the shape of an upside-down triangle. Each row of the triangle contains one more asterisk than the row above it, with the top row containing only one asterisk.

Updated on: 28-Mar-2023

223 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements