Harshad or Niven series: Difference between revisions

Content deleted Content added
Not a robot (talk | contribs)
Add PL/M
Hout (talk | contribs)
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 Nothing() if 0 == x else Just(divmod(x, 10))
return None if 0 == x else divmod(x, 10)
return sum(unfoldl(go)(n))
return sum(unfoldl(go)(n))




# TEST ----------------------------------------------------
# ------------------------- TEST -------------------------
# main :: IO ()
# main :: IO ()
def main():
def main():
Line 3,266: Line 3,266:




# GENERIC -------------------------------------------------
# ----------------------- 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.
Where these reduce a list to a summary value, unfoldl
by repeated application of f until no residue remains.
builds a list from a seed value.
Dual to fold/reduce.
Where f returns Just(a, b), a is appended to the list,
f returns either None or just (residue, value).
and the residual b is used as the argument for the next
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):
x, r = v, v
residueValue = f(v)
xs = []
while residueValue:
while True:
yield residueValue[1]
mb = f(x)
residueValue = f(residueValue[0])
return go
if mb.get('Nothing'):
return xs
else:
x, r = mb.get('Just')
xs.insert(0, r)
return xs
return lambda x: go(x)




# DISPLAY -------------------------------------------------
# ----------------------- DISPLAY ------------------------


# fTable :: String -> (a -> String) ->
# fTable :: String -> (a -> String) ->
# (b -> String) -> (a -> b) -> [a] -> String
# (b -> String) -> (a -> b) -> [a] -> String
def fTable(s):
def fTable(s):
'''Heading -> x display function -> fx display function ->
'''Heading -> x display function ->
f -> xs -> tabular string.
fx display function -> f -> xs -> tabular string.
'''
'''
def go(xShow, fxShow, f, xs):
def gox(xShow):
ys = [xShow(x) for x in xs]
def gofx(fxShow):
w = max(map(len, ys))
def gof(f):
return s + '\n' + '\n'.join(map(
def goxs(xs):
lambda x, y: y.rjust(w, ' ') + ' -> ' + fxShow(f(x)),
ys = [xShow(x) for x in xs]
xs, ys
w = max(map(len, ys))

))
def arrowed(x, y):
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(
map(arrowed, xs, ys)
)
return goxs
return gof
return gofx
return gox




Line 3,351: Line 3,333:
# MAIN ---
# MAIN ---
if __name__ == '__main__':
if __name__ == '__main__':
main()</lang>
main()
</lang>
{{Out}}
{{Out}}
<pre>Harshad or Niven series:
<pre>Harshad or Niven series: