Euler's sum of powers conjecture: Difference between revisions
Content deleted Content added
m →{{header|Haskell}}: Tidied. |
m →Python :: Composition of pure functions: Tidied. |
||
Line 3,070: | Line 3,070: | ||
# |
# ----------------------- 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 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): |
||
try: |
|||
if p(x) |
return Just(next(x for x in xs if p(x))) |
||
except StopIteration: |
|||
return Nothing() |
return Nothing() |
||
return |
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 ( |
|||
⚫ | |||
⚫ | |||
) else f(m.get('Just')) |
|||