Haskell Program to Print Left Triangle Star Pattern

In Haskell, to print the Left Triangle Star Pattern we will be using mapM_ function, replicate function and recursion. In the first example, we are going to use ( mapM_ putStrLn [replicate i '*' | i <- [1..n]]) function defined under main function and in the second example, we are going to use user-define, (printLeftTriangle n = mapM_ putStrLn [replicate i '*' | i <- [1..n]]) function . In the third example, we are going to use recursive (leftTriangle n = if n > 0 then leftTriangle (n-1) >> putStrLn (replicate n '*') else return ()) function.

Method 1: Printing Left Triangle Star Pattern using mapM_ function under main function

In this method, we first define the number of rows of the triangle as n (in this case, n = 5). We then use a list comprehension to generate a list of strings, where each string is a row of the triangle. Each row is generated using replicate i '*', which creates a string of i asterisks. Finally, we use mapM_ putStrLn to print each row of the triangle 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 left star pattern is to be printed.

• Step 3 − The mapM_ function is used to print the resultant left star pattern along with the replicate function as, mapM_ putStrLn [replicate i '*' | i <- [1..n]].

Example

In this example, the left triangle star pattern is printed using mapM_ function defined under main function.

main :: IO ()
main = do
let n = 5
mapM_ putStrLn [replicate i '*' | i <- [1..n]]


Output

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


Method 2: Printing Left Triangle Star Pattern using mapM_ function defined under user-defined function

In this method, the printLeftTriangle function takes an integer n as input and prints a left triangle star pattern of size n. The mapM_ function is used to apply the putStrLn function to each string in the list of stars generated by the list comprehension. The replicate function is used to generate a string of i stars, where i ranges from 1 to n. The list comprehension generates a list of strings of increasing length, which are then printed on separate lines using putStrLn.

Algorithm

• Step 1 − The user-defined, printLeftTriangle function is used to print the resultant left star pattern along with the replicate function as, printLeftTriangle n = mapM_ putStrLn [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 left star pattern is to be printed.

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

Example

In this example, the left triangle star pattern is printed using mapM_ and replicate function defined under a user-defined function.

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

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


Output

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


Method 3: Printing Left Triangle Star Pattern using recursion

In this method, the function takes an integer n as input and recursively prints n lines, each with n asterisks. The recursion stops when n is less than or equal to 0, and the return () statement ends the recursion.

Algorithm

• Step 1 − The user-defined, leftTriangle function is defined using recursion as, leftTriangle n = if n > 0 then leftTriangle (n-1) >> putStrLn (replicate n '*') else return ().

• 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 left star pattern is to be printed.

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

Example

In this example, the left triangle star pattern is printed using recursion.

leftTriangle :: Int -> IO ()
leftTriangle n = if n > 0 then leftTriangle (n-1) >> putStrLn (replicate n '*') else return ()

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


Output

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


Conclusion

The left triangle star pattern is a pattern of stars (*) arranged in a triangular shape with the left side being the longest side. In Haskell, we can use mapM_ , replicate function and recursion to implement the same.

Updated on: 28-Mar-2023

111 Views