Skip to content

Commit

Permalink
finished all scheme problems
Browse files Browse the repository at this point in the history
  • Loading branch information
tommyfan34 committed Mar 15, 2021
1 parent 943f08f commit 948131b
Show file tree
Hide file tree
Showing 27 changed files with 23 additions and 5 deletions.
Binary file modified scheme/.ok_history
Binary file not shown.
Binary file modified scheme/.ok_storage
Binary file not shown.
Binary file modified scheme/__pycache__/scheme.cpython-38.pyc
Binary file not shown.
14 changes: 14 additions & 0 deletions scheme/scheme.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,8 @@ def scheme_eval(expr, env, _=None): # Optional third argument is ignored
"*** YOUR CODE HERE ***"
operator = scheme_eval(first, env)
validate_procedure(operator)
if isinstance(operator, MacroProcedure):
return scheme_eval(operator.apply_macro(rest, env), env)
return scheme_apply(operator, rest.map(lambda x: scheme_eval(x, env)), env)
# END PROBLEM 4

Expand Down Expand Up @@ -475,6 +477,18 @@ def do_define_macro(expressions, env):
"""
# BEGIN Problem 20
"*** YOUR CODE HERE ***"
validate_form(expressions, 2)
target = expressions.first
if isinstance(target, Pair) and scheme_symbolp(target.first):
macro_name = target.first
args = target.rest
body = expressions.rest
macro = MacroProcedure(args, body, env)
env.bindings[macro_name] = macro
return macro_name
else:
bad_target = target.first if isinstance(target, Pair) else target
raise SchemeError('non-symbol: {0}'.format(bad_target))
# END Problem 20


Expand Down
14 changes: 9 additions & 5 deletions scheme/tests/18.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,20 @@
{
'code': r"""
scm> (define y 1)
y
1a9a3321b8b99a0f9291d89be986e74c
# locked
scm> (define f (mu (x) (+ x y)))
f
715124391110b4a3beec8c9ba1ec3097
# locked
scm> (define g (lambda (x y) (f (+ x x))))
g
4781badc0e77c1e0291e161d5c9bfa57
# locked
scm> (g 3 7)
13
ac8f8bf5e63b01fde95f04f5c6ce2820
# locked
""",
'hidden': False,
'locked': False
'locked': True
}
],
'scored': True,
Expand Down
Binary file modified scheme/tests/__pycache__/01.cpython-38.pyc
Binary file not shown.
Binary file modified scheme/tests/__pycache__/02.cpython-38.pyc
Binary file not shown.
Binary file modified scheme/tests/__pycache__/03.cpython-38.pyc
Binary file not shown.
Binary file modified scheme/tests/__pycache__/04.cpython-38.pyc
Binary file not shown.
Binary file modified scheme/tests/__pycache__/05.cpython-38.pyc
Binary file not shown.
Binary file modified scheme/tests/__pycache__/06.cpython-38.pyc
Binary file not shown.
Binary file modified scheme/tests/__pycache__/07.cpython-38.pyc
Binary file not shown.
Binary file modified scheme/tests/__pycache__/08.cpython-38.pyc
Binary file not shown.
Binary file modified scheme/tests/__pycache__/09.cpython-38.pyc
Binary file not shown.
Binary file modified scheme/tests/__pycache__/10.cpython-38.pyc
Binary file not shown.
Binary file modified scheme/tests/__pycache__/11.cpython-38.pyc
Binary file not shown.
Binary file modified scheme/tests/__pycache__/12.cpython-38.pyc
Binary file not shown.
Binary file modified scheme/tests/__pycache__/13.cpython-38.pyc
Binary file not shown.
Binary file modified scheme/tests/__pycache__/14.cpython-38.pyc
Binary file not shown.
Binary file modified scheme/tests/__pycache__/15.cpython-38.pyc
Binary file not shown.
Binary file modified scheme/tests/__pycache__/16.cpython-38.pyc
Binary file not shown.
Binary file modified scheme/tests/__pycache__/17.cpython-38.pyc
Binary file not shown.
Binary file modified scheme/tests/__pycache__/18.cpython-38.pyc
Binary file not shown.
Binary file modified scheme/tests/__pycache__/19.cpython-38.pyc
Binary file not shown.
Binary file modified scheme/tests/__pycache__/20.cpython-38.pyc
Binary file not shown.
Binary file modified scheme/tests/__pycache__/EC.cpython-38.pyc
Binary file not shown.
Binary file modified scheme/tests/__pycache__/eval_apply.cpython-38.pyc
Binary file not shown.

0 comments on commit 948131b

Please sign in to comment.