def __str__(self): function = str(self.operator) args = '(' + comma_separated(self.operands) + ')' if isinstance(self.operands, LambdaExpr): return '(' + function + ')' + args else: return function + args
def apply(self, arguments): """ 将参数绑定到lambda 的 变量中。 :param arguments: :return: """ if len(self.parameters) != len(arguments): raise TypeError( "Cannot match parameters {} to arguments {}".format( comma_separated(self.parameters), arguments)) env = self.parent.copy() for p, a in zip(self.parameters, arguments): env[p] = a return self.body.eval(env)
def __str__(self): body = str(self.body) if not self.parameters: return 'lambda: ' + body else: return 'lambda ' + comma_separated(body)
def __repr__(self): args = '(' + comma_separated([repr(a) for a in self.args]) + ')' return type(self).__name__ + args
def apply(self, arguments): for a in arguments: if not isinstance(a, Number): raise TypeError("Invalid argument {} to {}".format( comma_separated(arguments), self)) return Number(self.operator(*[a.value for a in arguments]))
def apply(self, arguments): raise TypeError("Cannot apply number{} to arguments {}".format( self.value, comma_separated(arguments)))