Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

evaluater problem

3 views
Skip to first unread message

fooo...@gmail.com

unread,
Jul 22, 2008, 5:50:53 AM7/22/08
to
I have to Write a procedure (rename exp) which renames all repeated
declarations
(parameters) in nested scopes. Modify “eval-substitution.scm”.
-------------------------------------------------------------------------------------
eval-substitution.scm”.
;~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
;~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ evaluator core
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
;~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
;;; Pre-conditions: The given expression is legal according to the
concrete syntax.
;;; Inner 'define' expressions are not legal.
;;; Evaluator does not support inner recursive procedures.
(define (eval exp)
(cond ((atomic? exp) (eval-atomic exp))
((special-form? exp) (eval-special-form exp))
((derived? exp) (eval-derived exp))
(else (apply-procedure (eval (operator exp))
(list-of-values (operands exp))))))

;~~~~~~~~~~~~~~~~~~~~ atomic ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
(define (atomic? exp)
(or (number? exp) (boolean? exp) (variable? exp)))

(define (eval-atomic exp)
(cond ((number? exp) exp)
((boolean? exp) exp)
((variable? exp) (lookup-variable-value exp))))

;~~~~~~~~~~~~~~~~~~~ special form ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

(define (special-form? exp)
(or (quoted? exp) (lambda? exp) (cond? exp) (definition? exp) (or?
exp)))

(define (eval-special-form exp)
(cond ((quoted? exp) (text-of-quotation exp))
((lambda? exp) exp)
((cond? exp) (eval-cond exp))
((definition? exp) (eval-definition exp))
((or? exp) (eval-or exp))))

(define (eval-cond exp)
(let ((clauses (cond-clauses exp)))
(cond ((null? clauses) 'unspecified)
((or (and (cond-last-clause? clauses) (eq? (cond-predicate
(cond-first-clause clauses)) 'else))
(eval (cond-predicate (cond-first-clause clauses))))
(eval-sequence (cond-actions (cond-first-clause (cond-
clauses exp)))))
(else (eval-cond (make-cond (cond-rest-clauses (cond-clauses
exp))))))))

;;; Purpose: Handle define expression. Bind a variable to a value in
the global environment.
(define (eval-definition exp)
(add-binding-to-environment (definition-variable exp) (eval
(definition-value exp)))
'ok)

(define (eval-or exp)
(if (null? (or-expressions exp))
#f
(let ((or-first-result (eval (or-first-expression exp))))
(if (or (= (length (or-expressions exp)) 1)
or-first-result)
or-first-result
(eval (make-or (or-rest-expression exp)))))))

;~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
;~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ derived expressions ( syntactic
sugars ) ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
;~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
(define (derived? exp)
(or (if? exp) (let? exp)))

(define (eval-derived exp)
(cond ((if? exp) (eval (if->cond exp)))
((let? exp) (eval (let->combination exp)))))

;;;Pre-condition: (if? exp)
(define (if->cond exp)
(let ((first-predicate (if-predicate exp))
(first-actions (list (if-consequent exp)))
(second-actions (list (if-alternative exp))) )
(let ( (first-clause (make-cond-clause first-predicate first-
actions))
(second-clause (make-cond-clause 'else second-actions)) )
(make-cond (list first-clause second-clause))
)))

;;; Pre-conditions: (let? exp)
(define (let->combination exp)
(make-application (make-lambda (let-variables exp) (let-body exp))
(let-initial-values exp)))

;~~~~~~~~~~~~~~~~~ application ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

(define (apply-procedure procedure arguments)
(cond ((lambda? procedure)
(eval-sequence
(substitute (lambda-body procedure) (lambda-parameters
procedure) arguments)))
((defined-proc-in-global? procedure)
(apply-primitive-procedure procedure arguments))
(else
(error
"eval: Illegal procedure" procedure))))

(define (eval-sequence exps)
(cond ((null? (cdr exps)) (eval (car exps)))
(else (eval (car exps))
(eval-sequence (cdr exps)))))

(define (apply-primitive-procedure proc args)
(apply proc args)) ; apply in underlying Scheme

;;; list-of-values is used in 'eval'.
(define (list-of-values exps)
(if (no-operands? exps)
()
(cons (eval (first-operand exps))
(list-of-values (rest-operands exps)))))

;~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
;~~~~~~~~~~~~~~~~ substitute procedures ~~~~~~~~~~~~~~~~~~~~~~~~~~~
;~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
;;; Pre-conditions: substitute is not performed on 'define' or 'let'
expressions
;;; or expression containing such sub-expressions.
(define (substitute exp vars vals)
(letrec (( substitute-var-val
(lambda (exp var val)
(cond ((or (number? exp) (quoted? exp) (boolean? exp))
exp)
((variable? exp)
(if (and (eq? exp var))
val ;; substitute free occurrence of var
with val
exp))
((lambda? exp)
(if (memq var (lambda-parameters exp))
exp
(make-lambda
(lambda-parameters exp)
(map (lambda (e) (substitute-var-val e var
val))
(lambda-body
exp)))) )
(else ; expression is a list of expressions:
application, cond.
(map (lambda(e) (substitute-var-val e var val))
exp)))) ))
(if (and (null? vars) (null? vals))
exp
(substitute
(substitute-var-val exp (car vars) (car vals))
(cdr vars)
(cdr vals))
) ))


;~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
;~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ global environment ADT
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
;~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

(define global-environment
(list
; primitive procedures
(list 'car car)
(list 'cdr cdr)
(list 'cons cons)
(list 'null? null?)
(list 'list list)
(list 'length length)
(list '+ +)
(list '* *)
(list '> >)
(list '< <)
(list '- -)
(list '= =)
(list 'expt expt)
))

(define (lookup-variable-value var)
(if (assoc var global-environment)
(cadr (assoc var global-environment))
(error "variable is not defined" var )))

(define (defined-proc-in-global? proc)
(member proc (map cadr global-environment)))

(define (add-binding-to-environment var val)
(set! global-environment (cons (list var val) global-environment)))
-------------------------------------------------------------------------------------------------------------------
The procedure may have internal procedures defined using letrec. Use
parser
procedures to get and build the expressions components.
Use the primitive procedure gensym to give a new variable name. For
example,
> (gensym)
g305
> (gensym)
g306
Here are some examples of how rename is expected to work:
>(rename 3)
3
>(rename '(quote r))
2
>r
>(rename '(x 3))
(x 3)
>(rename '(lambda(x y)
(* x y)))
(lambda (x y) (* x y))
>(rename '(lambda(x y)
(* ((lambda(y z)
(+ y x)) x y
)
y)))
(lambda (x y)
(* ((lambda (g423 z)
(+ g423 x)) x y
)
y))
>(rename '(lambda(x y)
(* ((lambda(y z)
(+ y ((lambda (x)
x)
x)))
x y)
y)))
(lambda (x y)
(* ((lambda (g446 z)
(+ g446 ((lambda (g445)
g445)
x)))
x y)
y))
Hint: review the substitute procedure and take ideas from it. The
procedure
rename may be similar to substitute and may even call substitute.
The contract
;;; 1. Signature: (rename exp)
;;; 2. Type: Expression -> Expression
;;; 3. Purpose: renames all repeated declarations (parameters) in
nested scopes.
;;; 4. Example: above.
;;; 5. Pre-conditions: exp is a legal expression.
;;; 6. Post-conditions: the result is an expression with identical
functionality to
exp.
;;; 7. Tests:

leppie

unread,
Jul 22, 2008, 9:50:32 AM7/22/08
to

Sorry, we cant do your homework for you...

fooo...@gmail.com

unread,
Jul 22, 2008, 10:28:26 AM7/22/08
to
On Jul 22, 6:50 am, leppie <xacc....@gmail.com> wrote:
> On Jul 22, 11:50 am, fooor...@gmail.com wrote:
>
>
>
>
>
> > I have to Write a procedure (rename exp) which renames all repeated
> > declarations
> > (parameters) in nested scopes. Modify “eval-substitution.scm”.
> > ---------------------------------------------------------------------------­----------
> > eval-substitution.scm”.
> > ;~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~­~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
> > ;~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ evaluator core
> > ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
> > ;~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~­~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
> > ;~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~­~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

> > ;~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ derived expressions ( syntactic
> > sugars ) ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
> > ;~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~­~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
> > ;~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~­~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

> > ;~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ global environment ADT
> > ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
> > ;~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~­~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
> > ---------------------------------------------------------------------------­----------------------------------------
> Sorry, we cant do your homework for you...- Hide quoted text -
>
> - Show quoted text -

i just want from you a main points what to do and not to solve the
exersice!
Regards

leppie

unread,
Jul 22, 2008, 11:24:23 AM7/22/08
to
On Jul 22, 4:28 pm, fooor...@gmail.com wrote:
> lotsa stuff

But yet you did not ask a specific question. What is the problem? Dont
just paste code.

fooo...@gmail.com

unread,
Jul 22, 2008, 3:06:33 PM7/22/08
to

which functions I have to use what is the algorithim to rename any
thing:
first u search for the name that u want to rename and then rename it
by subsitute the new name in the place of the old name by the func
set!?
does I on the way???

0 new messages