2021 fall cs61a dis 11
阿新 • • 發佈:2022-04-20
網址 https://inst.eecs.berkeley.edu/~cs61a/fa21/disc/disc11/#q1-using-pair
problem1:
probelm2:
def calc_eval(exp): if isinstance(exp, Pair): # Call expressions return calc_apply(calc_eval(exp.first), exp.rest.map(calc_eval)) elif exp in OPERATORS: # Names return OPERATORS[exp] else: # Numbers return exp def floor_div(expr): "*** YOUR CODE HERE ***" divident = expr.first expr = expr.rest while expr.rest != nil: divisor = expr.first dividend //= divisor expr = expr.rest return dividend
probelm3:
def calc_eval(exp): if isinstance(exp, Pair): if exp.first == 'and': # and expressions return eval_and(exp.rest) else: # Call expressions return calc_apply(calc_eval(exp.first), exp.rest.map(calc_eval)) elif exp in OPERATORS: # Names return OPERATORS[exp] else: # Numbers return exp def eval_and(operands): curr, val = operands, True while curr is not nil: val = calc_eval(curr.first) if val is False: return False curr = curr.rest return val
probelm4:
bindings = {} def calc_eval(exp): if isinstance(exp, Pair): if exp.first == 'and': # and expressions[paste your answer from the earlier] return eval_and(exp.rest) elif exp.first == 'define': # define expressions return eval_define(exp.rest) else: # Call expressions return calc_apply(calc_eval(exp.first), exp.rest.map(calc_eval)) elif exp in bindings: # Looking up variables "*** YOUR CODE HERE ***" return bindings[exp] elif exp in OPERATORS: # Looking up procedures return OPERATORS[exp] else: # Numbers return exp def eval_define(expr): bindings[expr.first] = expr.rest.first return expt.first
probelm5:
problem6: