Haskell Program to Print Upper Star Triangle Pattern


This tutorial will help us in printing the upper star triangle pattern using mapM function, forM function, and unliness functions in Haskell.

An upper star triangle pattern is a graphical representation of a triangle made up of asterisks or stars as shown below.

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

It's called an "upper" star triangle because the triangle starts at the top and the number of stars in each row decreases as we move down the triangle.

Algorithm

  • Step 1 − We will start with defining a user-defined function as printStars 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 upper triangle star pattern is to be printed.

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

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

Example 1

In this example, the printStars function takes an integer n as input and prints n stars in a single line. The printUpperTriangle function takes an integer n as input and calls the printStars function n times, each time with an increasing number of stars. The main function takes an integer input and calls the printUpperTriangle function with that input.

module Main where

printStars :: Int -> IO ()
printStars n = putStrLn (take n (cycle "*"))

printUpperTriangle :: Int -> IO ()
printUpperTriangle n = mapM_ printStars [1..n]

main :: IO ()
main = do
   let rowCount = 5
   printUpperTriangle rowCount

Output

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

Example 2

In this example, the function is defined using the mapM_ and replicate function to print the upper triangle star pattern.

module Main where

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

main :: IO ()
main = do
   let rowCount = 5
   printUpperTriangle rowCount

Output

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

Example 3

In this example, the Control.Monad module is imported, which provides the forM_ function used in the printUpperTriangle function. The printStars function takes an Int argument n and prints a row of n stars using the putStrLn function. The printUpperTriangle function takes an Int argument n and uses the forM_ function to call the printStars function n times, each time with an increasing number of stars.

import Control.Monad

printStars :: Int -> IO ()
printStars n = putStrLn (take n (cycle "*"))

printUpperTriangle :: Int -> IO ()
printUpperTriangle n = forM_ [1..n] (\i -> printStars i)

main :: IO ()
main = do
   let rowCount = 5
   printUpperTriangle rowCount

Output

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

Example 4

In this method, the generateRows function is used to generate a list of strings, where each string represents a row of stars. The unlines function is used to concatenate the strings into a single string, with newline characters inserted between each string.

module Main where

generateRows :: Int -> [String]
generateRows n = [replicate i '*' | i <- [1..n]]

printUpperTriangle :: Int -> IO ()
printUpperTriangle n = putStr (unlines (generateRows n))

main :: IO ()
main = do
   let rowCount = 5
   printUpperTriangle rowCount

Output

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

Conclusion

In Haskell, to print the upper star triangle pattern we can use MapM_ , forM_ or unlines functions.

Updated on: 06-Apr-2023

73 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements