Sierpinski triangle: Difference between revisions
Content deleted Content added
m →{{header|Python}}: (Added docstrings) |
|||
Line 3,216: | Line 3,216: | ||
and fold/reduce, wrapped as concatMap, can provide the list comprehensions too: |
and fold/reduce, wrapped as concatMap, can provide the list comprehensions too: |
||
<lang python> |
<lang python>from functools import (reduce) |
||
⚫ | |||
from functools import reduce |
|||
⚫ | |||
# sierpinski :: Int -> String |
# sierpinski :: Int -> String |
||
def sierpinski(n): |
def sierpinski(n): |
||
'''N rows of a Sierpinksi triangle.''' |
|||
def go(xs, i): |
def go(xs, i): |
||
s = ' ' * (2 ** i) |
s = ' ' * (2 ** i) |
||
Line 3,235: | Line 3,232: | ||
# concatMap :: (a -> [b]) -> [a] -> [b] |
# concatMap :: (a -> [b]) -> [a] -> [b] |
||
def concatMap(f): |
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: ( |
return lambda xs: ( |
||
reduce(add, map(f, xs), []) |
reduce(add, map(f, xs), []) |