Haskell Program to Print Square Star Pattern


In Haskell we can use internal functions like mapM, forM or recursive functions to print square star pattern.

A square star pattern is a two-dimensional pattern made up of stars (or asterisks, represented by the '*' symbol) arranged in the shape of a square as shown below.

****
****
****
****

The square pattern is formed by printing a specified number of lines, each containing a specified number of stars. The pattern can be of any size, with the number of lines and the number of stars in each line determining the overall size of the square pattern.

Algorithm

  • Step 1 − Define a user-defined function named generateLine

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

  • Step 3 − The variable named, “lines” is being initialized. It will hold the integer up to which the square 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, the generateLine function generates a line of the square pattern by repeating the '*' character n times. The printSquare function prints the pattern by repeating the line n times and using mapM_ putStrLn to print each line.

module Main where

generateLine :: Int -> String
generateLine n = replicate n '*'

printSquare :: Int -> IO ()
printSquare n = mapM_ putStrLn (replicate n (generateLine n))

main :: IO ()
main = do
   let lines = 4
   printSquare lines

Output

****
****
****
****

Example 2

In this example, the function is defined using the mapM_ and replicate function to print the square star pattern.

module Main where
printSquare :: Int -> IO ()
printSquare n = mapM_ putStrLn [replicate n '*' | _ <- [1..n]]

main :: IO ()
main = do
   let lines = 4
   printSquare lines

Output

****
****
****
****

Example 3

In this example, the generateLine function generates a line of the square pattern by repeating the '*' character n times. The printSquare function prints the pattern by repeating the line n times and using forM_ putStrLn to print each line.

module Main where

import Control.Monad (forM_)

printSquare :: Int -> IO ()
printSquare n = forM_ [1..n] $ \i -> putStrLn (replicate n '*')

main :: IO ()
main = do
   let lines = 4
   printSquare lines

Output

****
****
****
****

Example 4

In this example, the printLine function is used to print a line with n '*' characters, and the printSquare function is used to print the entire square pattern with n number of lines and m number of stars in each line. The function uses a conditional statement to stop the recursion when all n lines have been printed.

module Main where

printLine :: Int -> IO ()
printLine 0 = return ()
printLine n = do
   putChar '*'
   printLine (n - 1)

printSquare :: Int -> Int -> IO ()
printSquare n m
   | n == 0 = return ()
   | otherwise = do
      printLine m
      putStrLn ""
      printSquare (n - 1) m

main :: IO ()
main = do
   let lines = 4
   printSquare lines lines

Output

****
****
****
****

Conclusion

In Haskell, to print the square star pattern we can use MapM_ , forM_ or recursive functions.

Updated on: 06-Apr-2023

195 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements