0

The Catalan numbers satisfy the recurrence

Catalan Recurrence

Of course Catalan numbers have a closed form expression involving binomial coefficients. Also we can express C_n in terms of C_{n-1} alone. What I am wondering is how one can implement this kind of convolution in functional languages like SML or Haskell.

Guy Coder
  • 24,501
  • 8
  • 71
  • 136
Kuai
  • 135
  • 7

1 Answers1

1

Of course you can implement the catalan numbers in haskell (and I think as well the ml-family is powerful enough)!

But I guess this is not the answer you were looking for. So I hope you are familiar with the basic haskell syntax think of the catalan numbers as functions catalan :: Int -> Int any series of natural numbers is such a function (well for small indices). But as catalan numbers grow quite fast I will choose for the codomain of our function the type of Integers(= arbitrary big integral numbers).

catalan :: Int -> Integer
catalan 0 = 1
catalan n = sum [ ?catalan magic? | i <- [1..n]]

I know I almost solved the problem but there is still the catalan magic ;-) you have to do on your own.

But before I stop a few caveats

  • this version of calculating the catalan numbers is far from optimal or efficient
  • the case of negative input values is not taken care of.
epsilonhalbe
  • 15,637
  • 5
  • 46
  • 74
  • p.s. another solution could involve the functions `zipWith`, `reverse`, `(*)` and of course `sum` – epsilonhalbe Feb 08 '14 at 19:35
  • 1
    In Haskell, you'd probably just create an ininite list of all the catalan numbers. Assuming you restrict the type to `[Integer]`, that'd have the benefit of giving you memoization, too. – Carl Feb 08 '14 at 20:57
  • That's what I was intending to say in my comment - just less explicitly so that @Kuai can learn something – epsilonhalbe Feb 08 '14 at 21:20