How to resolve the algorithm Catalan numbers/Pascal's triangle step by step in the Haskell programming language

Published on 7 June 2024 03:52 AM

How to resolve the algorithm Catalan numbers/Pascal's triangle step by step in the Haskell programming language

Table of Contents

Problem Statement

Print out the first   15   Catalan numbers by extracting them from Pascal's triangle.

Pascal's triangle

Let's start with the solution:

Step by Step solution about How to resolve the algorithm Catalan numbers/Pascal's triangle step by step in the Haskell programming language

This Haskell code generates Pascal's triangle and uses it to calculate the Catalan numbers.

  1. Pascal's Triangle (pascal):

    • The pascal function generates Pascal's triangle as an infinite list of lists of integers.
    • Each row in the triangle is calculated by adding the adjacent elements of the previous row and inserting a 1 at both ends.
  2. Catalan Numbers (catalan):

    • The catalan function calculates the Catalan numbers by extracting them from Pascal's triangle using a method outlined by Grimaldi.
    • It takes the zip of Pascal's triangle rows with incremental indices, takes every other element, and then finds the difference between adjacent pairs.
  3. Main Function (main):

    • The main function:
      • Reads a list of integer arguments from the command line.
      • For each integer n, it calculates the n-th Catalan number using the catalan function.
      • Then, it takes the first n elements of the Catalan number sequence and prints them.

Usage:

To use this code, you can run it from the command line:

> haskell run program.hs 3 5 7
3
14
42

This will print the first three, five, and seven Catalan numbers, respectively.

Source code in the haskell programming language

import System.Environment (getArgs)

-- Pascal's triangle.
pascal :: [[Integer]]
pascal = [1] : map (\row -> 1 : zipWith (+) row (tail row) ++ [1]) pascal

-- The Catalan numbers from Pascal's triangle.  This uses a method from
-- http://www.cut-the-knot.org/arithmetic/algebra/CatalanInPascal.shtml
-- (see "Grimaldi").
catalan :: [Integer]
catalan = map (diff . uncurry drop) $ zip [0..] (alt pascal)
  where alt (x:_:zs) = x : alt zs -- every other element of an infinite list
        diff (x:y:_) = x - y
        diff (x:_)   = x

main :: IO ()
main = do
  ns <- fmap (map read) getArgs :: IO [Int]
  mapM_ (print . flip take catalan) ns


  

You may also check:How to resolve the algorithm Set right-adjacent bits step by step in the Perl programming language
You may also check:How to resolve the algorithm Constrained random points on a circle step by step in the Ada programming language
You may also check:How to resolve the algorithm Five weekends step by step in the Maxima programming language
You may also check:How to resolve the algorithm Variable-length quantity step by step in the Haskell programming language
You may also check:How to resolve the algorithm Read a specific line from a file step by step in the Racket programming language