Skip to content

Instantly share code, notes, and snippets.

@mdunsmuir
Created December 2, 2014 00:11
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mdunsmuir/6e24dcc7fe14cc8d61de to your computer and use it in GitHub Desktop.
Save mdunsmuir/6e24dcc7fe14cc8d61de to your computer and use it in GitHub Desktop.
module PascalsTriangle (pascalsTriangle) where
import Control.Applicative
import Test.QuickCheck
pascalsTriangle :: [[Int]]
pascalsTriangle = [1] : map row pascalsTriangle
where row prev = zipWith (+) (0 : prev) (prev ++ [0])
-- quickcheck stuff
data ChoosePair = ChoosePair Int Int deriving Show
instance Arbitrary ChoosePair where
arbitrary = flip suchThat (\(ChoosePair x y) -> y <= x) $
(\[x, y] -> ChoosePair x y ) <$> (vectorOf 2 (choose (0, 50)))
checkPascalsTriangle (ChoosePair n k) = choose n k == pascalsTriangle !! n !! k
where
choose n 0 = 1
choose 0 k = 0
choose n k = choose (n-1) (k-1) * n `div` k
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment