Euler's sum of powers conjecture: Difference between revisions

Content deleted Content added
Hout (talk | contribs)
Hout (talk | contribs)
Line 3,070: Line 3,070:




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



# Just :: a -> Maybe a
# Just :: a -> Maybe a
def Just(x):
def Just(x):
'''Constructor for an inhabited Maybe (option type) value.'''
'''Constructor for an inhabited Maybe (option type) value.
Wrapper containing the result of a computation.
'''
return {'type': 'Maybe', 'Nothing': False, 'Just': x}
return {'type': 'Maybe', 'Nothing': False, 'Just': x}




# Nothing :: Maybe a
# Nothing :: () -> Maybe a
def Nothing():
def Nothing():
'''Constructor for an empty Maybe (option type) value.'''
'''Constructor for an empty Maybe (option type) value.
Empty wrapper returned where a computation is not possible.
'''
return {'type': 'Maybe', 'Nothing': True}
return {'type': 'Maybe', 'Nothing': True}


Line 3,092: Line 3,095:
passed as an argument to the second.
passed as an argument to the second.
'''
'''
def go(f):
return lambda f: chain.from_iterable(map(f, xs))
return chain.from_iterable(map(f, xs))
return go




Line 3,104: Line 3,109:
def find(p):
def find(p):
'''Just the first element in the list that matches p,
'''Just the first element in the list that matches p,
or Nothing if no elements match.'''
or Nothing if no elements match.
'''
def go(xs):
def go(xs):
for x in xs:
try:
if p(x):
return Just(next(x for x in xs if p(x)))
return Just(x)
except StopIteration:
return Nothing()
return Nothing()
return lambda xs: go(xs)
return go




Line 3,117: Line 3,123:
'''Either the default value v, if m is Nothing,
'''Either the default value v, if m is Nothing,
or the application of f to x,
or the application of f to x,
where m is Just(x).'''
where m is Just(x).
'''
return lambda f: lambda m: v if m.get('Nothing') else (
f(m.get('Just'))
return lambda f: lambda m: v if (
None is m or m.get('Nothing')
)
) else f(m.get('Just'))