Pascal's triangle: Difference between revisions
m
→Functional Python: Tidied
m (Updated description and link for Fōrmulæ solution) |
m (→Functional Python: Tidied) |
||
Line 4,774:
===Functional===
▲With a scanl and a zipWith to hand, we can derive both finite and non-finite lists of pascal rows from a simple '''nextPascal''' step function:
{{Works with|Python|3.7}}
Line 4,789 ⟶ 4,787:
'''A row of Pascal's triangle
derived from a preceding row.'''
return
)▼
Line 4,804:
def go(a, _):
return nextPascal(a)
return
)
#
# main :: IO ()
def main():
Line 4,815 ⟶ 4,817:
- taking from a non-finite stream of rows,
- or constructing a finite list of rows.'''
print(
showPascal,
[
pascalTriangle()
7
),
finitePascalRows(7) # finite.
Line 4,840 ⟶ 4,843:
#
# center :: Int -> Char -> String -> String
Line 4,856 ⟶ 4,858:
# iterate :: (a -> a) -> a -> Gen [a]
def iterate(f):
'''An infinite list of repeated
applications of f to x.
def go(x):
v = x
Line 4,862 ⟶ 4,866:
yield v
v = f(v)
return
▲ accumulate(chain([a], xs), f)
▲ )
▲ xs[0:n]
▲ )
▲ list(map(f, xs, ys))
|