Haskell Infinite calculation :Pascal’s_triangle

–For details of Pascal’s_triangle go through : http://en.wikipedia.org/wiki/Pascal’s_triangle

— Haskell is good at lazy evaluation ,Using that here is infinite Pascal’s_triangle

pascalTriangle = lazyPascaltriangle [1]

lazyPascaltriangle xs = xs : lazyPascaltriangle((head xs):pairSum xs)

where pairSum [] = []
pairSum [x] = [x]
pairSum (x:y:xs) = (x+y):pairSum (y:xs)

–sample run
take 6 pascalTriangle
[[1],[1,1],[1,2,1],[1,3,3,1],[1,4,6,4,1],[1,5,10,10,5,1]]

Advertisements
This entry was posted in Uncategorized. Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s