Haskell Program to Print Right Triangle Star Pattern


In haskell we can use mapM, forM as well as recursive function to create a simple right triangle star pattern.

What is a Right Triangle Star Pattern?

A right triangle pattern is a series of asterisks or other characters arranged in a triangular shape. In the case of a right triangle pattern, the base of the triangle is the longest side and is aligned with the horizontal axis, while the other two sides form a right angle. The number of asterisks or characters in each row of the triangle decreases as you move up the triangle, so that the top row consists of a single asterisk or character.

Algorithm

  • Step 1 − The printTriangle 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 pattern is to be printed.

  • Step 3 − The variable named, “n” is being initialized. It will hold the integer up to which the 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, a printTriangle function is defined that takes an integer n as input and generates a list of strings, each containing a certain number of asterisks. The mapM_ function is used to apply the putStrLn function to each string in the list, effectively printing each line of the triangle.

module Main where

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

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

Output

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

Example 2

In this example, the forM_ function from the Control.Monad module is used to iterate over a range of integers from 1 to n and print a line of asterisks for each iteration. The putStrLn function is used to print each line, and the replicate function is used to generate a string of i asterisks, where i is the current iteration number.

import Control.Monad

printTriangle :: Int -> IO ()
printTriangle n = forM_ [1..n] $ \i -> putStrLn (replicate i '*')

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

Output

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

Example 3

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

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

main :: IO ()
main = do
   let n = 5
   printTriangle n 1

Output

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

Conclusion

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

Updated on: 24-Apr-2023

139 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements