Data commit

This commit is contained in:
Ingy döt Net 2023-07-01 11:58:00 -04:00
parent 7387c8f97b
commit cb5bb5e222
199093 changed files with 3378972 additions and 0 deletions

View file

@ -0,0 +1,4 @@
zapWith :: (a -> a -> a) -> [a] -> [a] -> [a]
zapWith f xs [] = xs
zapWith f [] ys = ys
zapWith f (x:xs) (y:ys) = f x y : zapWith f xs ys

View file

@ -0,0 +1,2 @@
extendWith f [] = []
extendWith f xs@(x:ys) = x : zapWith f xs ys

View file

@ -0,0 +1 @@
pascal = iterate (extendWith (+)) [1]

View file

@ -0,0 +1,2 @@
*Main> take 6 pascal
[[1],[1,1],[1,2,1],[1,3,3,1],[1,4,6,4,1],[1,5,10,10,5,1]]

View file

@ -0,0 +1,5 @@
-- generate next row from current row
nextRow row = zipWith (+) ([0] ++ row) (row ++ [0])
-- returns the first n rows
pascal = iterate nextRow [1]

View file

@ -0,0 +1,4 @@
pascal :: [[Integer]]
pascal =
(1 : [ 0 | _ <- head pascal])
: [zipWith (+) (0:row) row | row <- pascal]

View file

@ -0,0 +1,2 @@
*Pascal> take 5 <$> (take 5 $ triangle)
[[1,0,0,0,0],[1,1,0,0,0],[1,2,1,0,0],[1,3,3,1,0],[1,4,6,4,1]]

View file

@ -0,0 +1,5 @@
fac = product . enumFromTo 1
binCoef n k = fac n `div` (fac k * fac (n - k))
pascal = ((fmap . binCoef) <*> enumFromTo 0) . pred

View file

@ -0,0 +1,11 @@
*Main> putStr $ unlines $ map unwords $ map (map show) $ pascal 10
1
1 1
1 2 1
1 3 3 1
1 4 6 4 1
1 5 10 10 5 1
1 6 15 20 15 6 1
1 7 21 35 35 21 7 1
1 8 28 56 70 56 28 8 1
1 9 36 84 126 126 84 36 9 1