McNuggets problem: Difference between revisions

→‎Python List monad: (enumFromTo -> enumFromThenTo)
(→‎Python List monad: (enumFromTo -> enumFromThenTo))
Line 835:
 
Note that the innermost function wraps its results in a (potentially empty) list. The resulting list of lists, some empty, is then flattened by the concatenation component of '''bind'''.
<lang python>'''mcNuggets list monad'''
<lang python>from itertools import (chain, count, dropwhile, islice)
 
<lang python>from itertools import (chain, count, dropwhile, islice)
 
 
def main():
'''List monad equivalent of the list (or set) comprehension.'''
 
def size(n):
return enumFromToenumFromThenTo(0)(100 // n)(100)
 
mcNuggets = set(
bind(size(6))(lambda x:
bind(size(9))( lambda yx:
bind(size(209))(lambda z: (
lambda v=sum([6 * x, 9 *lambda y, 20 * z]): (
[v] if 101 > v else []bind(size(20))(
) lambda z: (
lambda v=sum([x, y, z]): (
)())))
[v] if 101 > v else []
)
)())))
)
 
Line 885 ⟶ 891:
list(range(m, 1 + n, next - m))
)
 
 
# enumFromTo :: (Int, Int) -> [Int]
def enumFromTo(m):
'''Integer enumeration from m to n.'''
return lambda n: list(range(m, 1 + n))
 
 
9,659

edits