Answered step by step
Verified Expert Solution
Question
1 Approved Answer
The Metacircular Evaluator ( Racket Language ) Add support for force and delay to your interpreter, where delay ' ed expressions are only evaluated once
The Metacircular Evaluator Racket Language
Add support for force and delay to your interpreter, where delay ed expressions are only evaluated once when force d
For full credit, a delay ed expression must be evaluated at most once. If your implementation evaluates a delayed expression every time it is forced but
is otherwise correct, you will receive half credit. Do not submit two implementation of force and delay.
It is highly recommended that you implement the nonmemoizing version of force and delay first. If you get stuck on the memoizing version without
having implemented the nonmemoizing version, stop what you're doing and get the nonmemoizing version working first.
The implementations of both the nonmemoizing and memoizing versions of force and delay were discussed in lecture. The easiest path to success
will follow that discussion. Although force and delay can be implemented using thunks, that approach is difficult to get right.
Your solution must demonstrate that you understand the underlying mechanisms for implementing lazy evaluation. Therefore, you may not use Racket's
force and delay or equivalent syntax macros in your solution to this problem. The homework template is set up to prevent you from accidentally
using Racket's force and delay.
If you have successfully implemented force and delay using the technique from lecture, then your evaluator should evaluate delay to
why However, force and delay are not identity functions! Although force delay e will have the same value as the expression e
implementing both force and delay as the identity function is incorrect. Similarly, if delay error throws an error, your implementation is not
correct and will score ; delay must delay evaluation.
Hint: Write a function delay lambda that rewrites a delay expression as described in lecture, leading to an implementation of delay as a derived
expression. Test your delaylambda independently to make sure it is performing the rewrite correctly, egdelaylambda delay
should evaluate to lambda why
define setupenvironment
let initialenv
extendenvironment primitiveprocedurenames
primitiveprocedureobjects
theemptyenvironment
definevariable! 'true true initialenv
definevariable! 'false false initialenv
initialenv
define primitiveprocedure? proc
taggedlist? proc 'primitive
define primitiveimplementation proccadr proc
define primitiveprocedures
list list 'car car
list cdr cdr
list 'cons cons
list 'null? null?
;; more primitives
define primitiveprocedurenames
map car
primitiveprocedures
define primitiveprocedureobjects
map lambda proclist 'primitive cadr proc
primitiveprocedures
define applyprimitiveprocedure proc args
apply primitiveimplementation proc args
define userprint object
if compoundprocedure? object
display list 'compoundprocedure
procedureparameters object
procedurebody object
display object
define topmceval exp
let val mceval exp setupenvironment
userprint val
define theglobalenvironment setupenvironment
define inputprompt
define driverloop
display inputprompt
when withhandlers
exn:fail? lambda exn
display "Error:
display exnmessage exn
newline
#t
let input read
if eofobject? input
begin
newline
#f
let output mceval input theglobalenvironment
userprint output
newline
#t
driverloop
define main argv
driverloop
provide mceval
setupenvironment
main
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started