- 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 Create Pyramid βandβ Pattern
In this tutorial, we are going to understand how to develop a Haskell program that will create a pyramid pattern of β&β using mapM, forM, and recursive function.
A pyramid β&β pattern is a design or arrangement of β&β or other symbols in the shape of a pyramid as shown below.
& &&& &&&&& &&&&&&& &&&&&&&&&
It is created by printing β&β or symbols in multiple rows, starting from the top and moving downwards. Each row contains one more symbol than the previous row, creating the illusion of a triangular shape. The pattern is usually symmetrical, with the number of β&β or symbols in each row equal to the row number, and the middle of each row lined up vertically.
Algorithm
Step 1 β Define a function using the internal 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 pyramid β&β pattern is to be printed.
Step 3 β The variable named, βnβ is being initialized. It will hold the integer up to which the pyramid 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 pyramid is defined that takes an integer n as an argument, and uses mapM_ to print each element of the list generated by the buildPyramid function. The buildPyramid function takes n as an argument and generates a list of strings that represent the pyramid β&β pattern. It uses a list comprehension to generate a string for each row of the pyramid, by concatenating two strings.
pyramid :: Int -> IO () pyramid n = mapM_ putStrLn $ buildPyramid n where buildPyramid :: Int -> [String] buildPyramid n = [replicate (n-i) ' ' ++ replicate (2*i-1) '&' | i <- [1..n]] main :: IO () main = do let n = 5 pyramid n
Output
& &&& &&&&& &&&&&&& &&&&&&&&&
Example 2
In this example, a function pyramid is defined that takes an integer n as an argument, and uses the forM_ function to iterate over the range [1..n]. The function passed to forM_ is an anonymous function (also known as a lambda function) that takes a single argument i, and uses putStrLn to print the string generated by concatenating two strings.
import Control.Monad pyramid :: Int -> IO () pyramid n = forM_ [1..n] $ \i -> putStrLn $ (replicate (n-i) ' ') ++ (replicate (2*i-1) '&') main :: IO () main = do let n = 5 pyramid 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 β&β 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.
drawLine :: Int -> Int -> IO () drawLine n k = do let spaces = replicate (n - k) ' ' let stars = replicate (2 * k - 1) '&' putStrLn (spaces ++ stars) drawPyramid :: Int -> IO () drawPyramid n = mapM_ (drawLine n) [1..n] main :: IO () main = do let n = 5 drawPyramid n
Output
& &&& &&&&& &&&&&&& &&&&&&&&&
Conclusion
In Haskell, to print the pyramid β&β pattern we can use MapM_ or forM_ functions or we can use a recursive function.