File: Catalan.hs

package info (click to toggle)
haskell-oeis 0.3.1-2
  • links: PTS, VCS
  • area: main
  • in suites: wheezy
  • size: 80 kB
  • sloc: haskell: 212; makefile: 2
file content (26 lines) | stat: -rw-r--r-- 714 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
import Math.OEIS
import Data.List (genericLength)

-- data-less binary trees.
data BTree = Empty | Fork BTree BTree  deriving Show

-- A list of all the binary trees with exactly n nodes.
listTrees :: Int -> [BTree]
listTrees 0 = [Empty]
listTrees n = [Fork left right | 
               k <- [0..n-1],
               left <- listTrees k,
               right <- listTrees (n-1-k) ]

-- HORRIBLY INEFFICIENT!!
countTrees :: Int -> Integer
countTrees = genericLength . listTrees

-- This is about the best we can do.
smallTreeCounts = map countTrees [0..10]

-- so... cheat!  Extend the sequence via the OEIS.
treeCounts = extendSequence smallTreeCounts

countTrees' :: Int -> Integer
countTrees' n = treeCounts !! n