tasks a-s

This commit is contained in:
Ingy döt Net 2013-04-10 23:57:08 -07:00
parent 47bf37c096
commit b83f433714
12433 changed files with 156208 additions and 123 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,5 @@
fac = product . enumFromTo 1
binCoef n k = (fac n) `div` ((fac k) * (fac $ n - k))
pascal n = map (binCoef $ n - 1) [0..n-1]

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