- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Haskell Program to Print Mirror Upper Star Triangle Pattern
In this article, we are going to learn how we can develop a haskell program to print mirror upper start triangle patterns using mapM function, and unliness functions. A mirror upper star triangle pattern is a pattern made up of stars (asterisks) that form a triangle shape, with the top of the triangle pointing upwards.
The following star pattern will give you a better understanding of mirror upper start triangle pattern.
* *** ***** ******* *********
The pattern is called "mirror" because the left and right sides of the triangle are symmetrical, creating a mirror image.
Algorithm
Step 1 − We will start with defining a user-defined function as printSpaces
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 mirror upper triangle star pattern is to be printed.
Step 3 − The variable named, “height” is being initialized. It will hold the integer up to which the mirror 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, a function printMirrorUpperStarTriangle is defined that takes in an integer n representing the height of the triangle, and prints a mirror upper star triangle pattern of that height. It does this by using the mapM_ function to iterate over a range of numbers [1..n], and for each number, it prints the appropriate number of spaces and stars to create the triangle pattern.
module Main where printSpaces :: Int -> IO () printSpaces n = putStr (replicate n ' ') printStars :: Int -> IO () printStars n = putStrLn (replicate n '*') printMirrorUpperStarTriangle :: Int -> IO () printMirrorUpperStarTriangle n = mapM_ (\x -> do printSpaces (n - x) printStars (2 * x - 1) ) [1..n] main :: IO () main = do let height = 5 printMirrorUpperStarTriangle height
Output
* *** ***** ******* *********
Example 2
In this example, the function is defined using the mapM_ and replicate function to print the mirror upper triangle star pattern.
module Main where printLine :: Int -> Int -> IO () printLine n x = putStrLn $ (replicate (n - x) ' ') ++ (replicate (2 * x - 1) '*') printMirrorUpperStarTriangle :: Int -> IO () printMirrorUpperStarTriangle n = mapM_ (printLine n) [1..n] main :: IO () main = do let height = 5 printMirrorUpperStarTriangle height
Output
* *** ***** ******* *********
Example 3
In this example, a recursive function printMirrorUpperStarTriangle is defined that takes in two arguments: n and x. n represents the height of the triangle, and x represents the current row.
module Main where printSpaces :: Int -> IO () printSpaces n = putStr (replicate n ' ') printStars :: Int -> IO () printStars n = putStrLn (replicate n '*') printMirrorUpperStarTriangle :: Int -> Int -> IO () printMirrorUpperStarTriangle n x | x > n = return () | otherwise = do printSpaces (n - x) printStars (2 * x - 1) printMirrorUpperStarTriangle n (x + 1) main :: IO () main = do let height = 5 printMirrorUpperStarTriangle height 1
Output
* *** ***** ******* *********
Conclusion
In Haskell, to print the upper star triangle pattern we can use MapM_ , replicate or recursive functions.