Sierpinski triangle: Difference between revisions
m
→{{header|Python}}: (Added docstrings)
m (→{{header|Python}}: (Added docstrings)) |
|||
Line 3,216:
and fold/reduce, wrapped as concatMap, can provide the list comprehensions too:
<lang python>
from operator import (add)▼
from functools import reduce
# sierpinski :: Int -> String
def sierpinski(n):
'''N rows of a Sierpinksi triangle.'''
def go(xs, i):
s = ' ' * (2 ** i)
Line 3,232 ⟶ 3,235:
# concatMap :: (a -> [b]) -> [a] -> [b]
def concatMap(f):
'''A concatenated list or string over which a function f
has been mapped.
The list monad can be derived by using an (a -> [b])
function which wraps its output in a list (using an
empty list to represent computational failure).
'''
return lambda xs: (
reduce(add, map(f, xs), [])
|