本文介绍了从其他环境返回变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在其他环境的范围内返回变量.我目前拥有的是

I'm attempting to return a variable in the scope of a different environment.What I have currently is

(define make-empty-env
   (lambda()
      (make-top-level-environment)
   )
)

,当您从解释程序调用它时会创建一个新环境即(定义环境(make-empty-env))

which creates a new environment when you call it from the interpreteri.e. (define env (make-empty-env))

如果我在"env"中将变量"a"定义为15,则我的目标是通过从用户初始环境调用的函数返回该值.

If I define the variable "a" as 15 in "env", my goal is to return this value through a function called from the user-initial-environment.

类似

(apply-env env'v)输出-> env范围内变量v的值.v可以在user-initial-environment中未定义,但是如果变量在env中存在,仍会返回一个值.

(apply-env env 'v) outputs -> value of variable v in scope of env. v can be undefined in user-initial-environment but would still return a value if the variable exists in env.

我尝试过:

(define apply-env
   (lambda (env v)
       (eval (+ v 0) env)
   )
)

无效,因为调用函数时传递了符号'v.我的主要问题是我不知道如何传递符号并将其像函数中的变量一样对待.这是一项作业,功能提示为:

which doesn't work since I'm passing a symbol 'v when I call the function. My main problem is that I don't know how to pass a symbol and treat it like a variable in the function. This is for a homework and the prompt for the function is:

返回环境env中的变量v的值.

Returns the value of variable v in environment env.

以下是对apply-env的一些调用:

Here are some calls to apply-env:

在test-env中,a = 1,b = 2

In test-env, a = 1, b = 2

(apply-env test-env'a) 1

(apply-env test-env 'a) 1

(apply-env test-env'b) 2

(apply-env test-env 'b) 2

我对计划还比较陌生,所以我可能缺少一些基本的基础知识,任何指针或帮助将不胜感激.

I'm relatively new to scheme so I'm probably missing some fundamental basics, any pointers or help would be very much appreciated.

推荐答案

据我所知,环境变量的设计实现取决于您.由于这是一个家庭作业问题,我假设您可以按照自己喜欢的任何方式进行操作(我认为).

As far as I know the design implementation for your environment variable is up to you. Since its a homework question I'm assuming that you can go about it any way you like (I assume).

所以可以说,您想将环境变量设置为a,它是2个元素的列表的列表.

So lets say you want to make your environment variable as a that is a list of list of 2 elements.

env -> ((var1,val1),(var2,val2),......,(varn.valn))

E.g.

env -> ((a,1),(b,2),(c,3))

现在,当您要初始创建"环境时,您只需要调用返回初始环境为的函数.现在我们的环境变量env只是一个空列表'().你可以做一个这样的功能

Now when you want to "create" your environment initially, you simply want to call a function which returns the initial environment which is empty. Now our environment variable env is just an empty list '(). You could make a function like this

(define (make-empty-env)
    (lambda ()
        '()
    )
)

现在在env中添加,删除和更新变量,您可以为基础数据结构实现carcdrin-env?之类的功能

Now to add, remove and update variables in env you can implement function like car and cdr and in-env? for the underlying data structure


;;; cdr like function for env
(define (cdr-env env)
    (cdr env)
)

;;; car like function for env
(define (car-env)
    (list (car env))
)

;;; Returns boolean for v in env
(define (in-env? env)
    (cond
        ((null? env)    ; If env is empty then v doesnt exist in env
            #f
        )
        ((equal? v (car (car env))) ; If first element of the first list in env matches with v 
            #t
        )
        (else       ; Recursive step to find if v is in the remaining enviornment env
            (in-env? v (cdr env))
        )
    )
)

使用这些功能,您可以相对轻松地实现env的插入,检索和更新功能.

Using these functions you can relatively easily implement your insertion, retrieval and updation functions for env.


;;; Update the value variable v to val in env. This function assumes that you are calling it in the insert-env function
(define (update-env v val env)
    (cond 
        ((null? env)
            '()
        )
        ((equal? v (car (car env)))
            (append (cdr env) (list (list v val)))
        )
        (else
            (append 
                (update-env v val (cdr env)) 
                (list (list (car (car env)) (car (cdr (car env)))))
            )
        )
    )
)


;;; add a variable-value pair (v,val) to env, also checks if variable already exists in pair. If so then updates it
(define (insert-env v val env)
    (cond 
        ((in-env? v env)
            (update-env v val env)
        )
        (else  ; Add to env the 2 element list (v val)
            (append env (list (list v val)))
        )
    )
)

;;; Gets the value of variable v or returns an error message
(define apply-env
    (lambda (env v)
        (cond 
            ((null? env)    ; If env is empty then no variablles exist for the name v
                (error "appply-env: found empty enviroment")
            )
            ((equal? (car (car env)) v) ; First element of the first list is v [matching var found in env]
                (car (cdr (car env))) ; Second element of that list is value of v
            )
            (else       ; Else look for v in the rest of the env list
                (apply-env (cdr env) v) 
            )
        )
    )
)

希望这会有所帮助!

这篇关于从其他环境返回变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-27 17:41