Skip to content

Commit

Permalink
did first 9 qs of project scheme
Browse files Browse the repository at this point in the history
  • Loading branch information
Kettenkrado committed Apr 24, 2024
1 parent 8e8e064 commit 456b552
Show file tree
Hide file tree
Showing 44 changed files with 119 additions and 144 deletions.
Binary file added projects/scheme/.ok_history
Binary file not shown.
1 change: 1 addition & 0 deletions projects/scheme/.ok_messages
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
�]�.
Binary file added projects/scheme/.ok_storage
Binary file not shown.
Binary file added projects/scheme/__pycache__/pair.cpython-310.pyc
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file added projects/scheme/__pycache__/ucb.cpython-310.pyc
Binary file not shown.
14 changes: 11 additions & 3 deletions projects/scheme/scheme_classes.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,13 +26,16 @@ def __repr__(self):
def define(self, symbol, value):
"""Define Scheme SYMBOL to have VALUE."""
# BEGIN PROBLEM 1
"*** YOUR CODE HERE ***"
self.bindings[symbol] = value
# END PROBLEM 1

def lookup(self, symbol):
"""Return the value bound to SYMBOL. Errors if SYMBOL is not found."""
# BEGIN PROBLEM 1
"*** YOUR CODE HERE ***"
if symbol in self.bindings:
return self.bindings[symbol]
if self.parent:
return self.parent.lookup(symbol)
# END PROBLEM 1
raise SchemeError('unknown identifier: {0}'.format(symbol))

Expand All @@ -51,7 +54,12 @@ def make_child_frame(self, formals, vals):
if len(formals) != len(vals):
raise SchemeError('Incorrect number of arguments to function call')
# BEGIN PROBLEM 8
"*** YOUR CODE HERE ***"
child_frame = Frame(self)
while formals is not nil:
child_frame.bindings[formals.first] = vals.first
formals = formals.rest
vals = vals.rest
return child_frame
# END PROBLEM 8

##############
Expand Down
26 changes: 22 additions & 4 deletions projects/scheme/scheme_eval_apply.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,9 @@ def scheme_eval(expr, env, _=None): # Optional third argument is ignored
return scheme_forms.SPECIAL_FORMS[first](rest, env)
else:
# BEGIN PROBLEM 3
"*** YOUR CODE HERE ***"
operator = scheme_eval(expr.first, env)
operands = expr.rest.map(lambda x: scheme_eval(x, env))
return scheme_apply(operator, operands, env)
# END PROBLEM 3

def scheme_apply(procedure, args, env):
Expand All @@ -44,11 +46,21 @@ def scheme_apply(procedure, args, env):
assert False, "Not a Frame: {}".format(env)
if isinstance(procedure, BuiltinProcedure):
# BEGIN PROBLEM 2
"*** YOUR CODE HERE ***"
py_args = []
while args is not nil:
# A sidenote by the author:
# here we should view the args as a list of self-evaluating values.
# because in Q3 we are asked to use The map method of Pair returns a new Scheme list
# constructed by applying a one-argument function to every item in a Scheme list.
# or we could get an error: "xxx is not callable: xxx"
py_args.append(args.first)
args = args.rest
if procedure.need_env:
py_args.append(env)
# END PROBLEM 2
try:
# BEGIN PROBLEM 2
"*** YOUR CODE HERE ***"
return procedure.py_func(*py_args)
# END PROBLEM 2
except TypeError as err:
raise SchemeError('incorrect number of arguments: {0}'.format(procedure))
Expand Down Expand Up @@ -79,7 +91,13 @@ def eval_all(expressions, env):
2
"""
# BEGIN PROBLEM 6
return scheme_eval(expressions.first, env) # replace this with lines of your own code
last_expression = expressions
if last_expression is nil:
return
while last_expression.rest is not nil:
scheme_eval(last_expression.first, env)
last_expression = last_expression.rest
return scheme_eval(last_expression.first, env)
# END PROBLEM 6


Expand Down
15 changes: 12 additions & 3 deletions projects/scheme/scheme_forms.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,14 @@ def do_define_form(expressions, env):
# assigning a name to a value e.g. (define x (+ 1 2))
validate_form(expressions, 2, 2) # Checks that expressions is a list of length exactly 2
# BEGIN PROBLEM 4
"*** YOUR CODE HERE ***"
symbol = signature
value = scheme_eval(expressions.rest.first, env)
# sidenote: the signature has the form of Pair(A, Pair(B, nil)),
# where: A is the symbol being bound, B is an expression whose value should be evaluated and bound to A
# what we should evaluate is B, not Pair(B, nil)
# ...this is a little strange...
env.define(symbol, value)
return symbol
# END PROBLEM 4
elif isinstance(signature, Pair) and scheme_symbolp(signature.first):
# defining a named procedure e.g. (define (f x y) (+ x y))
Expand All @@ -56,7 +63,8 @@ def do_quote_form(expressions, env):
"""
validate_form(expressions, 1, 1)
# BEGIN PROBLEM 5
"*** YOUR CODE HERE ***"
# same as problem 4, the structure: Pair(A, nil), where: A is the quoted expression
return expressions.first
# END PROBLEM 5

def do_begin_form(expressions, env):
Expand All @@ -82,7 +90,8 @@ def do_lambda_form(expressions, env):
formals = expressions.first
validate_formals(formals)
# BEGIN PROBLEM 7
"*** YOUR CODE HERE ***"
body = expressions.rest
return LambdaProcedure(formals, body, env)
# END PROBLEM 7

def do_if_form(expressions, env):
Expand Down
28 changes: 9 additions & 19 deletions projects/scheme/tests/01.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,24 +9,17 @@
>>> global_frame = create_global_frame()
>>> global_frame.define("x", 3)
>>> global_frame.parent is None
b1796eff8a8e977439f97b5c6881a282
# locked
True
>>> global_frame.lookup("x")
3c7e8a3a2176a696c3a66418f78dff6b
# locked
3
>>> global_frame.define("x", 2)
>>> global_frame.lookup("x")
2b7cdec3904f986982cbd24a0bc12887
# locked
2
>>> global_frame.lookup("foo")
ec908af60f03727428c7ee3f22ec3cd8
# locked
# choice: None
# choice: SchemeError
# choice: 3
SchemeError
""",
'hidden': False,
'locked': True,
'locked': False,
'multiline': False
},
{
Expand All @@ -35,18 +28,15 @@
>>> first_frame.define("x", 3)
>>> second_frame = Frame(first_frame)
>>> second_frame.parent == first_frame
b1796eff8a8e977439f97b5c6881a282
# locked
True
>>> second_frame.define("y", False)
>>> second_frame.lookup("x")
3c7e8a3a2176a696c3a66418f78dff6b
# locked
3
>>> second_frame.lookup("y")
96ae38315990d5fb27de4225d8b470ba
# locked
False
""",
'hidden': False,
'locked': True,
'locked': False,
'multiline': False
},
{
Expand Down
15 changes: 6 additions & 9 deletions projects/scheme/tests/02.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,23 +10,21 @@
>>> twos = Pair(2, Pair(2, nil))
>>> plus = BuiltinProcedure(scheme_add) # + procedure
>>> scheme_apply(plus, twos, env) # Type SchemeError if you think this errors
46beb7deeeb5e9af1c8d785b12558317
# locked
4
""",
'hidden': False,
'locked': True,
'locked': False,
'multiline': False
},
{
'code': r"""
>>> env = create_global_frame()
>>> plus = BuiltinProcedure(scheme_add) # + procedure
>>> scheme_apply(plus, nil, env) # Remember what (+) evaluates to in scheme
a384c59daad07475a000a57b0b47b74f
# locked
0
""",
'hidden': False,
'locked': True,
'locked': False,
'multiline': False
},
{
Expand All @@ -35,11 +33,10 @@
>>> twos = Pair(2, Pair(2, nil))
>>> oddp = BuiltinProcedure(scheme_oddp) # odd? procedure
>>> scheme_apply(oddp, twos, env) # Type SchemeError if you think this errors
ec908af60f03727428c7ee3f22ec3cd8
# locked
SchemeError
""",
'hidden': False,
'locked': True,
'locked': False,
'multiline': False
},
{
Expand Down
22 changes: 8 additions & 14 deletions projects/scheme/tests/03.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,22 +8,18 @@
'code': r"""
>>> expr = read_line('(+ 2 2)')
>>> scheme_eval(expr, create_global_frame()) # Type SchemeError if you think this errors
46beb7deeeb5e9af1c8d785b12558317
# locked
4
>>> scheme_eval(Pair('+', Pair(2, Pair(2, nil))), create_global_frame()) # Type SchemeError if you think this errors
46beb7deeeb5e9af1c8d785b12558317
# locked
4
>>> expr = read_line('(+ (+ 2 2) (+ 1 3) (* 1 4))')
>>> scheme_eval(expr, create_global_frame()) # Type SchemeError if you think this errors
4c5d1a42692bacbca88ab48bbcf75c52
# locked
12
>>> expr = read_line('(yolo)')
>>> scheme_eval(expr, create_global_frame()) # Type SchemeError if you think this errors
ec908af60f03727428c7ee3f22ec3cd8
# locked
SchemeError
""",
'hidden': False,
'locked': True,
'locked': False,
'multiline': False
}
],
Expand All @@ -40,14 +36,12 @@
{
'code': r"""
scm> (* (+ 3 2) (+ 1 7)) ; Type SchemeError if you think this errors
a692eb3d6b9f6889d113635424465221
# locked
40
scm> (1 2) ; Type SchemeError if you think this errors
ec908af60f03727428c7ee3f22ec3cd8
# locked
SchemeError
""",
'hidden': False,
'locked': True,
'locked': False,
'multiline': False
},
{
Expand Down
22 changes: 9 additions & 13 deletions projects/scheme/tests/04.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
{
'cases': [
{
'answer': 'e92e90f58a272e7a74651635251ade14',
'answer': 'Pair(A, Pair(B, nil)), where: A is the symbol being bound, B is an expression whose value should be evaluated and bound to A',
'choices': [
r"""
Pair(A, Pair(B, nil)), where:
Expand Down Expand Up @@ -34,20 +34,20 @@
"""
],
'hidden': False,
'locked': True,
'locked': False,
'multiline': False,
'question': 'What is the structure of the expressions argument to do_define_form?'
},
{
'answer': '0ed53dce7bacc4766422abc478c5c895',
'answer': 'define',
'choices': [
'make_child_frame',
'define',
'lookup',
'bindings'
],
'hidden': False,
'locked': True,
'locked': False,
'multiline': False,
'question': r"""
What method of a Frame instance will bind
Expand All @@ -63,20 +63,16 @@
{
'code': r"""
scm> (define size 2)
cc3c061fb8167d02a4ddda1f1c19966e
# locked
size
scm> size
2b7cdec3904f986982cbd24a0bc12887
# locked
2
scm> (define x (+ 7 3))
38ba916dc1f41eb239567ee41a251ecd
# locked
x
scm> x
4bc2fb48972a5d1ec1201b01e766a044
# locked
10
""",
'hidden': False,
'locked': True,
'locked': False,
'multiline': False
},
{
Expand Down
15 changes: 6 additions & 9 deletions projects/scheme/tests/05.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
{
'cases': [
{
'answer': 'fd4dd892ccea3adcf9446dc4a9738d47',
'answer': 'Pair(A, nil), where: A is the quoted expression',
'choices': [
r"""
Pair('quote', Pair(A, nil)), where:
Expand All @@ -25,7 +25,7 @@
"""
],
'hidden': False,
'locked': True,
'locked': False,
'multiline': False,
'question': 'What is the structure of the expressions argument to do_quote_form?'
}
Expand All @@ -38,18 +38,15 @@
{
'code': r"""
>>> do_quote_form(Pair(3, nil), global_frame)
3c7e8a3a2176a696c3a66418f78dff6b
# locked
3
>>> do_quote_form(Pair('hi', nil), global_frame)
95448591e64e04a7a7885d5fb9b45583
# locked
'hi'
>>> expr = Pair(Pair('+', Pair('x', Pair(2, nil))), nil)
>>> do_quote_form(expr, global_frame) # Make sure to use Pair notation
2301ee746b57783004f00f39498fdaed
# locked
Pair('+', Pair('x', Pair(2, nil)))
""",
'hidden': False,
'locked': True,
'locked': False,
'multiline': False
}
],
Expand Down
Loading

0 comments on commit 456b552

Please sign in to comment.