Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>I will add another answer in order not to overload my first one and because this one includes both my initial idea and the valuable input from <a href="https://stackoverflow.com/users/355230/martineau">@martineau</a> (and this time hopefully PEP-8 compliant).</p> <p>Let's define a function like this and let's try to guess its arity:</p> <pre><code>import inspect import operator class UnguessableArityException(Exception): pass class Function: def __init__(self, f, arity = ...): self.f = f self.arity = arity if arity == ...: argspec = inspect.getfullargspec(f) if argspec.varargs or argspec.kwonlyargs: raise UnguessableArityException() self.arity = len(argspec.args) def __call__(self, *args): return self.f(*args) def op(self, g, op): if isinstance(g, Function): return Function(lambda *args: op(self.f(*args[:self.arity]), g.f(*args[self.arity:])), self.arity + g.arity) return Function(lambda *args: op(self.f(*args), g), self.arity) def rop(self, g, op): return Function(lambda *args: op(g, self.f(*args)), self.arity) def __add__(self, g): return self.op(g, operator.add) def __radd__(self, g): return self.rop(g, operator.add) def __mul__(self, g): return self.op(g, operator.mul) def __rmul__(self, g): return self.rop(g, operator.mul) def __truediv__(self, g): return self.op(g, operator.truediv) def __rtruediv__(self, g): return self.rop(g, operator.truediv) </code></pre> <p>Now we basically just need to define identity:</p> <pre><code>#define identity i = Function(lambda x: x) # λx.x </code></pre> <p>And using the identity you can start to build your "growing" ("appending") functions.</p> <pre><code>#now you can start building your function f = i * i # λxy.x*y print(f(3, 2)) #later in your code you need to "append" another term λxy.x*y f = f + f # λxyza.x*y+z*a print(f(3,2,1,4)) #even later, you need to "append" a term λxy.x/y f = f + i / i # λxyzabc.x*y+z*a+b/c print(f(3,2,1,4,14,2)) #works also with constants f = 2 * f # λxyzabc.2*(x*y+z*a+b/c) print(f(3,2,1,4,14,2)) </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload