Harshad or Niven series: Difference between revisions
Content deleted Content added
Not a robot (talk | contribs) Add PL/M |
m →Python: Functional: Updated primitives |
||
Line 3,239: | Line 3,239: | ||
'''Sum of the decimal digits of n.''' |
'''Sum of the decimal digits of n.''' |
||
def go(x): |
def go(x): |
||
return |
return None if 0 == x else divmod(x, 10) |
||
return sum(unfoldl(go)(n)) |
return sum(unfoldl(go)(n)) |
||
# |
# ------------------------- TEST ------------------------- |
||
# main :: IO () |
# main :: IO () |
||
def main(): |
def main(): |
||
Line 3,266: | Line 3,266: | ||
# |
# ----------------------- GENERIC ------------------------ |
||
# Just :: a -> Maybe a |
|||
def Just(x): |
|||
'''Constructor for an inhabited Maybe (option type) value. |
|||
Wrapper containing the result of a computation. |
|||
''' |
|||
return {'type': 'Maybe', 'Nothing': False, 'Just': x} |
|||
# Nothing :: Maybe a |
|||
def Nothing(): |
|||
'''Constructor for an empty Maybe (option type) value. |
|||
Empty wrapper returned where a computation is not possible. |
|||
''' |
|||
return {'type': 'Maybe', 'Nothing': True} |
|||
# take :: Int -> [a] -> [a] |
# take :: Int -> [a] -> [a] |
||
Line 3,297: | Line 3,281: | ||
# unfoldl(lambda x: Just(((x - 1), x)) if 0 != x else Nothing())(10) |
|||
# -> [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] |
|||
# unfoldl :: (b -> Maybe (b, a)) -> b -> [a] |
# unfoldl :: (b -> Maybe (b, a)) -> b -> [a] |
||
def unfoldl(f): |
def unfoldl(f): |
||
'''A lazy (generator) list unfolded from a seed value |
|||
'''Dual to reduce or foldl. |
|||
by repeated application of f until no residue remains. |
|||
Dual to fold/reduce. |
|||
f returns either None or just (residue, value). |
|||
For a strict output list, wrap the result with list() |
|||
application of f. |
|||
When f returns Nothing, the completed list is returned. |
|||
''' |
''' |
||
def go(v): |
def go(v): |
||
residueValue = f(v) |
|||
while residueValue: |
|||
yield residueValue[1] |
|||
residueValue = f(residueValue[0]) |
|||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
# |
# ----------------------- DISPLAY ------------------------ |
||
# fTable :: String -> (a -> String) -> |
# fTable :: String -> (a -> String) -> |
||
# |
# (b -> String) -> (a -> b) -> [a] -> String |
||
def fTable(s): |
def fTable(s): |
||
'''Heading -> x |
'''Heading -> x display function -> |
||
fx display function -> f -> xs -> tabular string. |
|||
''' |
''' |
||
def |
def gox(xShow): |
||
def gofx(fxShow): |
|||
def gof(f): |
|||
def goxs(xs): |
|||
ys = [xShow(x) for x in xs] |
|||
w = max(map(len, ys)) |
|||
⚫ | |||
⚫ | |||
return lambda xShow: lambda fxShow: lambda f: lambda xs: go( |
|||
return y.rjust(w, ' ') + ' -> ' + ( |
|||
xShow, fxShow, f, xs |
|||
fxShow(f(x)) |
|||
) |
|||
⚫ | |||
return s + '\n' + '\n'.join( |
|||
⚫ | |||
⚫ | |||
⚫ | |||
return gof |
|||
⚫ | |||
⚫ | |||
Line 3,351: | Line 3,333: | ||
# MAIN --- |
# MAIN --- |
||
if __name__ == '__main__': |
if __name__ == '__main__': |
||
main() |
main() |
||
</lang> |
|||
{{Out}} |
{{Out}} |
||
<pre>Harshad or Niven series: |
<pre>Harshad or Niven series: |