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.

Updated on: 06-Apr-2023

89 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements