Skip to content

Instantly share code, notes, and snippets.

@hakunin
Created February 10, 2010 21:02
Show Gist options
  • Select an option

  • Save hakunin/300831 to your computer and use it in GitHub Desktop.

Select an option

Save hakunin/300831 to your computer and use it in GitHub Desktop.
-- This is how Haskell newb (me) writes Pascal's triangle
-- evolution of each row
pascal [1] = [1, 1]
pascal (x:xs) = pascal2 (x:xs) [1]
pascal2 (x:y:xs) o | xs == [] = (1:(x+y):o)
| otherwise = pascal2 (y:xs) ((x+y):o)
-- whole triangle
pascaln n = pascaln2 n [] [1]
pascaln2 n t r | n == 0 = reverse (r:t)
| n > 0 = pascaln2 (n-1) (r:t) (pascal r)
-- usage
Main> pascaln 5
[[1],[1,1],[1,2,1],[1,3,3,1],[1,4,6,4,1]]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment