Haskell Program to Print Star Pascal’s Triangle


In Haskell we can use mapM function and forM function to print a star Pascal’s Triangle.

A star Pascal's triangle is a variation of the traditional Pascal's triangle that uses asterisks (or stars) instead of numbers to form a triangular pattern as shown below.

    *
   * *
  *   *
 *     *
*       *

Pascal's triangle is a triangular array of numbers, where each number in the triangle is the sum of the two numbers above it. In a star Pascal's triangle, each number is represented by an asterisk, so the triangle is made up of asterisks instead of numbers.

Algorithm

  • Step 1 − We will start with defining a user-defined function printLine

  • 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 star Pascal’s triangle pattern is to be printed.

  • Step 3 − The variable named, “lines” is being initialized. It will hold the integer up to which the star Pascal’s triangle 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 printLine function takes a list of integers line and the number of spaces spaces, and it prints spaces number of spaces followed by the elements of line as either a star or a space. The nextRow function generates the next row of the star Pascal's triangle using the previous row. The printTriangle function maps the printLine function over a list of integers [1..n], where n is the number of lines specified by the user. The argument n - x passed to the printLine function ensures that the number of spaces decreases with each line, while the take x [1,1..] expression generates the list of elements for each line.

module Main where

nextRow :: [Int] -> [Int]
nextRow line = zipWith (+) (0 : line) (line ++ [0])

generateTriangle :: [[Int]]
generateTriangle = iterate nextRow [1]

printTriangle :: Int -> IO ()
printTriangle n = mapM_ printLine (take n generateTriangle)
   where
      printLine line = putStrLn (replicate (n - length line) ' ' ++ unwords (map (\x -> if x == 1 then "*" else " ") line))

main :: IO ()
main = do
   let lines = 5
   printTriangle lines

Output

    *
   * *
  *   *
 *     *
*       *

Example 2

In this example, the function is defined using the mapM_ and replicate function to print the star Pascal’s triangle pattern.

module Main where

nextRow :: [Int] -> [Int]
nextRow line = zipWith (+) (0 : line) (line ++ [0])

generateTriangle :: [[Int]]
generateTriangle = iterate nextRow [1]

printTriangle :: Int -> IO ()
printTriangle n = mapM_ printLine (take n generateTriangle)
   where
      printLine line = putStrLn (replicate (n - length line) ' ' ++ unwords (map (\x -> if x == 1 then "*" else " ") line))

main :: IO ()
main = do
   let lines = 5
   printTriangle lines

Output

    *
   * *
  *   *
 *     *
*       *

Example 3

In this example, the forM_ function is used to print the triangle. The forM_ function allows us to perform an action for each element in a list and discards the result of the action. This approach is useful for printing the triangle because it allows us to easily loop through each row and each element in each row, printing the spaces and asterisks as needed.

module Main where
import Control.Monad

nextRow :: [Int] -> [Int]
nextRow xs = zipWith (+) (0 : xs) (xs ++ [0])

generateTriangle :: Int -> [[Int]]
generateTriangle n = take n (iterate nextRow [1])

printTriangle :: Int -> IO ()
printTriangle n = forM_ (generateTriangle n) $ \row -> do
   forM_ [1..n - length row] $ \_ -> putStr " "
   forM_ row $ \x -> if x == 1 then putStr "*" else putStr "  "
   putStrLn "  "

main :: IO ()
main = do
   let lines = 5
   printTriangle lines

Output

    *
   * *
  *   *
 *     *
*       *

Conclusion

In Haskell, to print the star Pascal’s triangle pattern we can use MapM_ , replicate or forM_ functions.

Updated on: 06-Apr-2023

318 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements