Left factorials: Difference between revisions

Content added Content deleted
(Added Quackery.)
Line 2,679: Line 2,679:


{{Trans|Haskell}}
{{Trans|Haskell}}
<lang python>"""Left factorials"""
<lang python>'''Left factorials'''


from itertools import (accumulate, chain, count, islice)
from itertools import (accumulate, chain, count, islice)
Line 2,687: Line 2,687:
# leftFact :: [Integer]
# leftFact :: [Integer]
def leftFact():
def leftFact():
'''Left factorial series defined in terms of the factorial series'''
'''Left factorial series defined in terms
of the factorial series.
return scanl(add)(0)(
fact()
'''
return accumulate(
chain([0], fact()), add
)
)


Line 2,695: Line 2,697:
# fact :: [Integer]
# fact :: [Integer]
def fact():
def fact():
'''Factorial series – a non-finite list'''
'''The factorial series.
'''
return scanl(mul)(1)(
enumFrom(1)
return accumulate(
chain([1], count(1)), mul
)
)




# TEST ----------------------------------------------------
# ------------------------- TEST -------------------------
# main :: IO ()
# main :: IO ()
def main():
def main():
Line 2,725: Line 2,728:




# GENERIC -------------------------------------------------
# ----------------------- GENERIC ------------------------


# compose (<<<) :: (b -> c) -> (a -> b) -> a -> c
# compose (<<<) :: (b -> c) -> (a -> b) -> a -> c
Line 2,731: Line 2,734:
'''Function composition.'''
'''Function composition.'''
return lambda f: lambda x: g(f(x))
return lambda f: lambda x: g(f(x))


# enumFrom :: Enum a => a -> [a]
def enumFrom(x):
'''A non-finite stream of enumerable values,
starting from the given value.'''
return count(x) if isinstance(x, int) else (
map(chr, count(ord(x)))
)




# scanl :: (b -> a -> b) -> b -> [a] -> [b]
# scanl :: (b -> a -> b) -> b -> [a] -> [b]
def scanl(f):
def scanl(f):
'''scanl is like reduce, but returns a succession of
'''scanl is like reduce, but defines a succession of
intermediate values, building from the left.'''
intermediate values, building from the left.
'''
return lambda a: lambda xs: (
def go(a):
accumulate(chain([a], xs), f)
)
def g(xs):
return accumulate(chain([a], xs), f)
return g
return go