Haskell Program to Print Downward Triangle Star Pattern


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

Method 1: Printing Downward 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 downward triangle star pattern is to be printed.

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

Example

In this example, the downward 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 ...
*****
****
***
**
*

Method 2: Printing Downward Triangle Star Pattern using mapM_ and replicate the function

In this method, the printDownwardTriangle function takes an integer n as input and prints the downward triangle star pattern using a list comprehension. The list comprehension generates a list of strings, where each string is a row of the triangle consisting of * characters. The number of * characters in each row is determined by the list of integers [n,n-1..1], which counts down from n to 1. The replicate function is used to create a string of * characters of the appropriate length for each row. Finally, mapM_ putStrLn is used to print each row on a separate line.

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 downward triangle star pattern is to be printed.

  • Step 3 − The User defined function is defined using mapM_ and replicate

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

Example 1

In this example, the printDownwardTriangle takes an Int as its argument and returns an IO action that prints out the downward 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
  printDownwardTriangle n

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

Output

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

Example 2

In this example, the printDownwardTriangle takes an Int as its argument and returns an IO action that prints out the downward 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 $ downwardTriangle n

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

Output

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

Conclusion

The downward triangle star pattern is a series of rows of asterisks, with each row having one fewer asterisk than the previous row. The rows are arranged in a downward pointing triangle shape, with the first row having the most asterisks and the last row having only one asterisk.

Updated on: 28-Mar-2023

91 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements