![]() |
![]() |
|
Due Dec.13 (Thu) 17:00
Submit:
T6.rkt and T6-test.rkt (or, T6-java.jar) on D2L,
and hardcopy with the changed code from T4 (tagged
Extra Credit: I'll take your percentage score on this assignment, and replace your lowest homework-score.
We continue to build on
the T language implementation from previous homeworks
(T2 sol'n posted;
T4 sol'n discussed on Monday and full source by Mon 17:00.
Note that T5 can be based off of the T2 solution for full credit).
You may implement this homework in either Java or Racket (or another language, if you clear it with me).
Label each section of lines you change with a comment “
T5 (25pts;
This problem and the next are really the crux of the project.)
Deferred evaluation:
T5 doesn't add any new syntax,
but it is a different evaluation model which
will give us more expressive semantics.
There are two problems1 with the substitution approach used in T2–T4: we fundamentaly can't create recursive functions, and it's hopeless for ever assigning to variables. We solve these problems with deferred substitution:
Your test cases should include
a recursive function,
as well as the example below.
Also, since
Re-naming 'eval': Uh-oh, it'd be a pain to have to go and adapt all our existing test-cases foreval , to take an extra argument. I would advise something like3: search-replace all occurrences ofeval in your tests to be instead calling “eval-helper ”. Make that a function which simply callseval with an empty-environment. Then, go changeeval to have a second input, and add some two-argument test-cases as just mentioned.
A step sideways: This algorithm as described lets us add recursive functions, but it also doesn't handle some expressions that T4 did! For example,so make-adder be all func m -> func n -> #n boii m# in call call make-adder(3)(4) gives an error "unbound identifier: m" if no substitution has been done. The problem will be fixed in T6: callingcall make-adder(3) returns a function whose body still includesm andn , but lacks the fact that we'd like it'sm to be bound to 3. One approach might be to haveeval return both a value and a set of bindings to use that value with. T6 will take a slightly different approach, storing the necessary bindings inside the function-representation.)
Note that this gives us dynamic scoping (which we'll mention in class):
so m be all 100
in so addM be all func x -> #x boii m#
in #so m be all 5 in call addM(3)
boii
so m be all 4 in call addM(3)#
|
(10pts) Modify your function-structures so that they include one extra piece of information: the bindings in effect when the function was declared. This is the function's closure.
Be sure to make a careful suite of test-cases,
to test for various ways of capturing bindings in different closures.
(For example, consider the
To think about:
Hmm, when we first parse our expression, we'll
create function-expressions,
but (since we're not
Only later, when we
subtlety: This means that a function won't quite evaluate to itself anymore — it'll evaluate to a struct that has the same parameter and body as the original (parsed) structure, but a fully fleshed-out, non-dummy closure.
(10pts) Note that getting recursive functions to work is a bit tricky: their environment (closure) needs to include its own name! That is, we'll have eventually end up with a function-struct whose closure-field is a list containing the function-struct. That's not a problem in racket, no more than it is in Java -- racket struct values are actually references-to-structs, just like in Java4. However, it will require mutation (read on).
The tricky bit is that when
when you're
If you want to use mutation in your racket-implementation,
use Advanced Student language.
This language level includes
Since the mutators return
(define times-called 0)
(define (triplify-and-print n)
(begin (printf "We use `begin` to call a function for a side-effect *and* return a value too.\n")
(set! times-called (add1 times-called))
(printf "This function has been called ~a time~a.\n"
times-called
(if (= times-called 1) "" "s"))
(* 3 n)))
(triplify-and-print 5)
(triplify-and-print 2)
|
Btw, it's worth noting that in full-racket,
This page licensed CC-BY 4.0 Ian Barland Page last generated | Please mail any suggestions (incl. typos, broken links) to ibarland |