Catalan numbers: Difference between revisions

Content added Content deleted
(add more versions)
m (→‎{{header|Haskell}}: Tidied, applied Ormolu.)
Line 2,294: Line 2,294:


=={{header|Haskell}}==
=={{header|Haskell}}==
<lang haskell>-- Three infinite lists, corresponding to the three definitions in the problem
<lang haskell>-- Three infinite lists, corresponding to the three
-- statement.
-- definitions in the problem statement.


cats1 :: [Integer]
cats1 :: [Integer]
cats1 =
cats1 = (\n -> product [n + 2 .. 2 * n] `div` product [1 .. n]) <$> [0 ..]
(div . product . (enumFromTo . (2 +) <*> (2 *)))
<*> (product . enumFromTo 1) <$> [0 ..]


cats2 :: [Integer]
cats2 :: [Integer]
cats2 =
cats2 = 1 : fmap (\n -> sum $ zipWith (*) (reverse (take n cats2)) cats2) [1 ..]
1 :
fmap
(\n -> sum (zipWith (*) (reverse (take n cats2)) cats2))
[1 ..]


cats3 :: [Integer]
cats3 :: [Integer]
cats3 =
cats3 = scanl (\c n -> c * 2 * (2 * n - 1) `div` (n + 1)) 1 [1 ..]
scanl
(\c n -> c * 2 * (2 * n - 1) `div` succ n)
1
[1 ..]


main :: IO ()
main :: IO ()