Example #1
0
 def apply(self, arguments):
     """
     >>> from reader import read
     >>> add_lambda = read('lambda x, y: add(x, y)').eval(global_env)
     >>> add_lambda.apply([Number(1), Number(2)])
     Number(3)
     >>> add_lambda.apply([Number(3), Number(4)])
     Number(7)
     >>> sub_lambda = read('lambda add: sub(10, add)').eval(global_env)
     >>> sub_lambda.apply([Number(8)])
     Number(2)
     >>> add_lambda.apply([Number(8), Number(10)]) # Make sure you made a copy of env
     Number(18)
     >>> read('(lambda x: lambda y: add(x, y))(3)(4)').eval(global_env)
     Number(7)
     >>> read('(lambda x: x(x))(lambda y: 4)').eval(global_env)
     Number(4)
     """
     if len(self.parameters) != len(arguments):
         raise TypeError("Cannot match parameters {} to arguments {}".format(
             comma_separated(self.parameters), comma_separated(arguments)))
     d_dict = self.parent.copy()
     #如果不copy,python能够修改这个值
     #更新
     for i in range(0, len(self.parameters)):
         d_dict[self.parameters[i]] = arguments[i]
     #求值
         return self.body.eval(d_dict)
Example #2
0
 def apply(self, arguments):
     """
     >>> from reader import read
     >>> add_lambda = read('lambda x, y: add(x, y)').eval(global_env)
     >>> add_lambda.apply([Number(1), Number(2)])
     Number(3)
     >>> add_lambda.apply([Number(3), Number(4)])
     Number(7)
     >>> sub_lambda = read('lambda add: sub(10, add)').eval(global_env)
     >>> sub_lambda.apply([Number(8)])
     Number(2)
     >>> add_lambda.apply([Number(8), Number(10)]) # Make sure you made a copy of env
     Number(18)
     >>> read('(lambda x: lambda y: add(x, y))(3)(4)').eval(global_env)
     Number(7)
     >>> read('(lambda x: x(x))(lambda y: 4)').eval(global_env)
     Number(4)
     """
     if len(self.parameters) != len(arguments):
         raise TypeError("Cannot match parameters {} to arguments {}".format(
             comma_separated(self.parameters), comma_separated(arguments)))
     env = self.parent.copy()
     for parameter, argument in zip(self.parameters, arguments):
         env[parameter] = argument
     return self.body.eval(env)
Example #3
0
 def apply(self, arguments):
     """
     >>> from reader import read
     >>> add_lambda = read('lambda x, y: add(x, y)').eval(global_env)
     >>> add_lambda.apply([Number(1), Number(2)])
     Number(3)
     >>> add_lambda.apply([Number(3), Number(4)])
     Number(7)
     >>> sub_lambda = read('lambda add: sub(10, add)').eval(global_env)
     >>> sub_lambda.apply([Number(8)])
     Number(2)
     >>> add_lambda.apply([Number(8), Number(10)]) # Make sure you made a copy of env
     Number(18)
     >>> read('(lambda x: lambda y: add(x, y))(3)(4)').eval(global_env)
     Number(7)
     >>> read('(lambda x: x(x))(lambda y: 4)').eval(global_env)
     Number(4)
     """
     if len(self.parameters) != len(arguments):
         raise TypeError("Cannot match parameters {} to arguments {}".format(
             comma_separated(self.parameters), comma_separated(arguments)))
     diccopy = self.parent.copy()
     temp = 0
     while temp < len(arguments):
         if not self.parameters[temp] in diccopy:
             diccopy.setdefault(self.parameters[temp],arguments[temp])
         else:
             diccopy[self.parameters[temp]] = arguments[temp]
             temp += 1
     return diccopy[str(self.body.operator)].apply([operand.eval(diccopy) for operand in self.body.operands])
Example #4
0
 def __str__(self):
     function = str(self.operator)
     args = '(' + comma_separated(self.operands) + ')'
     if isinstance(self.operator, LambdaExpr):
         return '(' + function + ')' + args
     else:
         return function + args
Example #5
0
 def apply(self, arguments):
     """
     >>> from reader import read
     >>> add_lambda = read('lambda x, y: add(x, y)').eval(global_env)
     >>> add_lambda.apply([Number(1), Number(2)])
     Number(3)
     >>> add_lambda.apply([Number(3), Number(4)])
     Number(7)
     >>> sub_lambda = read('lambda add: sub(10, add)').eval(global_env)
     >>> sub_lambda.apply([Number(8)])
     Number(2)
     >>> add_lambda.apply([Number(8), Number(10)]) # Make sure you made a copy of env
     Number(18)
     """
     if len(self.parameters) != len(arguments):
         raise TypeError("Cannot match parameters {} to arguments {}".format(
             comma_separated(self.parameters), comma_separated(arguments)))
     "*** YOUR CODE HERE ***"
     parent_copy = self.parent.copy() #create a parent copy
     for i in range(0, len(arguments)):
         parent_copy[self.parameters[i]] = arguments[i]
     return self.body.eval(parent_copy)
Example #6
0
 def apply(self, arguments):
     raise TypeError("Cannot apply number {} to arguments {}".format(
         self.value, comma_separated(arguments)))
Example #7
0
 def __str__(self):
     body = str(self.body)
     if not self.parameters:
         return 'lambda: ' + body
     else:
         return 'lambda ' + comma_separated(self.parameters) + ': ' + body
Example #8
0
 def __repr__(self):
     """
     Returns how this value is written in our Python representation.
     """
     args = '(' + comma_separated([repr(arg) for arg in self.args]) + ')'
     return type(self).__name__ + args
Example #9
0
 def __str__(self):
     body = str(self.body)
     if not self.parameters:
         return 'lambda: ' + body
     else:
         return 'lambda ' + comma_separated(self.parameters) + ': ' + body
Example #10
0
 def apply(self, arguments):
     for arg in arguments:
         if type(arg) != Number:
             raise TypeError("Invalid arguments {} to {}".format(
                 comma_separated(arguments), self))
     return Number(self.operator(*[arg.value for arg in arguments]))
Example #11
0
 def apply(self, arguments):
     raise TypeError("Cannot apply number {} to arguments {}".format(
         self.value, comma_separated(arguments)))
Example #12
0
 def __str__(self):
     body = str(self.body)
     if not self.parameters:
         return "lambda: " + body
     else:
         return "lambda " + comma_separated(self.parameters) + ": " + body
Example #13
0
 def __str__(self):
 	operator_str = str(self.operator)
 	operands_str = comma_separated(self.operands)
 	if isinstance(self.operator, LambdaExpr):
 		return '(' + operator_str + ')' + '(' + operands_str + ')'
 	return operator_str + '(' + operands_str + ')'
Example #14
0
 def apply(self, arguments):
     for arg in arguments:
         if type(arg) != Number:
             raise TypeError("Invalid arguments {} to {}".format(
                 comma_separated(arguments), self))
     return Number(self.operator(*[arg.value for arg in arguments]))
Example #15
0
 def __repr__(self):
     args = '(' + comma_separated([repr(arg) for arg in self.args]) + ')'
     return type(self).__name__ + args
Example #16
0
 def __repr__(self):
     args = '(' + comma_separated([repr(arg) for arg in self.args]) + ')'
     return type(self).__name__ + args
Example #17
0
 def apply(self, arguments):
     # 很自然地,Number不可被调用,即不可被apply。只有callable才能被apply。
     raise TypeError("Cannot apply number {} to arguments {}".format(
         self.value, comma_separated(arguments)))