Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

The Metacircular Evaluator ( Racket Language ) Add support for and and or to your interpreter by modifying mceval. rkt . Be sure your implementation

The Metacircular Evaluator (Racket Language)
Add support for and and or to your interpreter by modifying mceval. rkt. Be sure your implementation adheres to the R5RS standard. Pay careful
attention to how the arguments to and and or are evaluated and the value of the and or or expression-the language standard describes a recursive
procedure for evaluating and and or expressions. Also make sure you evaluate each subexpression at most once.
You will probably want to use the last-exp?, first-exp, and rest-exps helper functions.
Hint: Before you start, think carefully about whether or not and and or must be implemented as special forms. When must a new language construct
be a special form?(define (self-evaluating? exp)
(cond ((number? exp) true)
((string? exp) true)
((char? exp) true)
((boolean? exp) true)
(else false)))
(define (quoted? exp)
(tagged-list? exp 'quote))
(define (text-of-quotation exp)(cadr exp))
(define (tagged-list? exp tag)
(if (pair? exp)
(eq?(car exp) tag)
false))
(define (variable? exp)(symbol? exp))
(define (assignment? exp)
(tagged-list? exp 'set!))
(define (assignment-variable exp)(cadr exp))
(define (assignment-value exp)(caddr exp))
(define (definition? exp)
(tagged-list? exp 'define))
(define (definition-variable exp)
(if (symbol?(cadr exp))
(cadr exp)
(caadr exp)))
(define (definition-value exp)
(if (symbol?(cadr exp))
(caddr exp)
(make-lambda (cdadr exp)
(cddr exp))))
(define (lambda? exp)(tagged-list? exp 'lambda))
(define (lambda-parameters exp)(cadr exp))
(define (lambda-body exp)(cddr exp))
(define (make-lambda parameters body)
(cons 'lambda (cons parameters body)))
(define (if? exp)(tagged-list? exp 'if))
(define (if-predicate exp)(cadr exp))
(define (if-consequent exp)(caddr exp))
(define (if-alternative exp)
(if (not (null?(cdddr exp)))
(cadddr exp)
'false))
(define (make-if predicate consequent alternative)
(list 'if predicate consequent alternative))
(define (begin? exp)(tagged-list? exp 'begin))
(define (begin-actions exp)(cdr exp))
(define (last-exp? seq)(null?(cdr seq)))
(define (first-exp seq)(car seq))
(define (rest-exps seq)(cdr seq))
(define (sequence->exp seq)
(cond ((null? seq) seq)
((last-exp? seq)(first-exp seq))
(else (make-begin seq))))
(define (make-begin seq)(cons 'begin seq))
(define (application? exp)(pair? exp))
(define (operator exp)(car exp))
(define (operands exp)(cdr exp))
(define (no-operands? ops)(null? ops))
(define (first-operand ops)(car ops))
(define (rest-operands ops)(cdr ops))
(define (cond? exp)(tagged-list? exp 'cond))
(define (cond-clauses exp)(cdr exp))
(define (cond-else-clause? clause)
(eq?(cond-predicate clause) 'else))
(define (cond-predicate clause)(car clause))
(define (cond-actions clause)(cdr clause))
(define (cond->if exp)
(expand-clauses (cond-clauses exp)))
(define (expand-clauses clauses)
(if (null? clauses)
'false ; no else clause
(let ((first (car clauses))
(rest (cdr clauses)))
(if (cond-else-clause? first)
(if (null? rest)
(sequence->exp (cond-actions first))
(error "ELSE clause isn't last -- COND->IF"
clauses))
(make-if (cond-predicate first)
(sequence->exp (cond-actions first))
(expand-clauses rest))))))
image text in transcribed

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

More Books

Students also viewed these Databases questions