Haskell Program to Print Mirror Lower Star Triangle Pattern


In haskell, we can use unlines and replicate functions to print mirror lower star in triangle pattern. In the first example, we are going to use ( mirrorLowerTriangle n = unlines [replicate i '*' | i <- [1..n]] ++ unlines (reverse [replicate i '*' | i <- [2..n]])) function and in the second example, we are going to use, (lowerMirrorTriangle n = unlines $ [replicate i '*' | i <- [1..n]] ++ [replicate i '*' | i <- [n-1,n-2..1]]) function.

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

  • Step 3 − The lowerMirrorTriangle function is used to print the resultant mirror lower star traingle pattern along with the unlines and replicate function

Example 1

In this example, the mirror lower star triangle pattern is printed using reverse, unlines and replicate function.

main = do
  let n = 5
  putStrLn $ mirrorLowerTriangle n

mirrorLowerTriangle :: Int -> String
mirrorLowerTriangle n = unlines [replicate i '*' | i <- [1..n]] ++ unlines (reverse [replicate i '*' | i <- [2..n]])

Output

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

Example 2

In this example, the mirror lower star triangle pattern is printed using reverse, unlines and replicate function defined under main function.

main = do
  let n = 5
  putStrLn $ unlines [replicate i '*' | i <- [1..n]] ++ unlines (reverse [replicate i '*' | i <- [2..n]])

Output

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

Example 3

In this example, the mirror lower star triangle pattern is printed using unlines and replicate function.

main = do
  let n = 5
  putStrLn $ lowerMirrorTriangle n

lowerMirrorTriangle :: Int -> String
lowerMirrorTriangle n = unlines $ [replicate i '*' | i <- [1..n]] ++ [replicate i '*' | i <- [n-1,n-2..1]]

Output

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

Example 4

In this example, the mirror lower star triangle pattern is printed using unlines and replicate function defined under main function.

main = do
  let n = 5
  putStrLn $ unlines $ [replicate i '*' | i <- [1..n]] ++ [replicate i '*' | i <- [n-1,n-2..1]]

Output

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

Conclusion

Mirror lower star triangle pattern is a pattern of stars (*) in the shape of a triangle, where the triangle is mirrored along the horizontal axis, with the apex of the triangle at the bottom. In other words, the pattern consists of a series of rows of stars, where each row has one more star than the previous row, and the rows are mirrored along the horizontal axis, so that the longest row of stars is in the middle of the pattern, and the shortest rows are at the top and bottom of the pattern.

Updated on: 28-Mar-2023

81 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements