简单scheme计算器-python实现

class nil(object):
    """The empty list"""
    def __len__(self):
        return 0
    def map(self,fn):
        return self
    def __repr__(self):
        return 'nil'
nil=nil() # nil now refers to a single instance of nil class
class Pair(object):
    def __init__(self,first,second=nil):
        self.first=first
        self.second=second
    def __repr__(self):
        return 'Pair({0},{1})'.format(repr(self.first),repr(self.second))
    def __len__(self):
        n,second=1,self.second
        while isinstance(second,Pair):
            n+=1
            second=second.second
        if second is not nil:
            raise TypeError("length attempted on improper list")
        return n
    def __getitem__(self,k):
        if k<0:
            raise IndexError("negative index into list")
        j,y=0,self
        while j<k:
            if y.second is nil:
                raise IndexError("list index out of bounds")
            elif not isinstance(y.second,Pair):
                raise TypeError("ill-formed list")
            j,y=j+1,y.second
        return y.first
    def map(self,fn):
        """Return a Scheme list after mapping Python function fn over self."""
        mapped=fn(self.first)
        if self.second is nil or isinstance(self.second,Pair):
            return Pair(mapped,self.second.map(fn))
        else:
            raise TypeError("ill-formed list")
    def to_py_list(self):
        """Return a python list containing the elements of this Scheme list"""
        y,result=self,[]
        while y is not nil:
            result+=[y.first]
            if not isinstance(y.second,Pair) and y.second is not nil:
                raise TypeError("ill-formed list")
            y=y.second
        return result
def calc_eval(exp):
    if not isinstance(exp,Pair):
        return exp
    else:
        operator,operands=exp.first,exp.second
        args=operands.map(calc_eval).to_py_list()
    return calc_apply(operator,args)
def calc_apply(operator,args):
    if operator=='+':
        return sum(args)
    elif operator=='-':
        if len(args)==1:
            return -args[0]
        else:
            return sum(args[0],[-args for args in args[1:]])
    elif operator=='*':
        return reduce(mul,args,1)
    elif operato=='/':
        if len(args)==1:
            raise ValueError("args must bigger than 2")
        elif 0 in args:
            raise ZeroDivisionError("division by zero")
        else:
            reduce(mul,args,1)
exp=Pair('+',Pair(1,Pair(2,Pair(3,Pair(4,nil)))))
a=calc_eval(exp)
print(a)

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值