使用Scheme

时间:2018-02-14 02:40:31

标签: oop functional-programming closures scheme racket

我希望创建类似于OOP

中的对象或环境的东西

这就是我的想法:

(define env (variable 'x 10 env))

我在那里定义一个env并在该环境中创建一个值为10的变量。

我还希望能够调用该环境中的值。例如

(get-value env 'x)
> 10

我能理解的最多的是它涉及closures,但我不知道从哪里开始

2 个答案:

答案 0 :(得分:1)

实现这一目标的最简单方法是使用alist。上述variableget-value可以定义如下:

(define (variable name value env) (cons (cons name value) env))
(define (get-value name env) (cond ((assq name env) => cdr) (else #f)))

如果要隐藏空列表,初始环境为'(),您也可以这样定义:

(define initial-env '())

答案 1 :(得分:1)

有很多方法可以做到这一点。 classical way是使用alist:

(define (variable name value env)
  (cons (cons name value) env))

(define (get-value name env)
  (let ((val (assq name env)))
    (if val 
        (cdr val)
        (error "Unbound variable" name)))) ;  for r6rs use raise

;; the empty environment
(define the-empty-environment '())

Al Petrofsky做了eiod(Eval在一个定义中)这样做:

;; actually called extend
(define (variable name value env)
  (lambda (i) (if (eq? name i) value (env i)))

;; just a wrapper, no need for it since you can just call env with the name
(define (get-value name env)
  (env name))

;; the empty environment
(define (the-empty-environment i) 
  (error "Unbound variable" i))

SICP中,您有包含绑定的框架:

;; actually called extend-environment
(define (variables vars vals env)
  (if (= (length vars) (length vals))
      (cons (make-frame vars vals) env)
      (if (< (length vars) (length vals))
          (error "Too many arguments supplied" vars vals)
          (error "Too few arguments supplied" vars vals))))

;; actualy called lookup-variable-value 
(define (get-value var env)
  (define (env-loop env)
    (define (scan vars vals)
      (cond ((null? vars)
             (env-loop (enclosing-environment env)))
            ((eq? var (car vars))
             (car vals))
            (else (scan (cdr vars) (cdr vals)))))
    (if (eq? env the-empty-environment)
        (error "Unbound variable" var)
        (let ((frame (first-frame env)))
          (scan (frame-variables frame)
                (frame-values frame)))))
  (env-loop env))


;; the empty environment
(define the-empty-environment '())

;; referenced
(define (enclosing-environment env) (cdr env))
(define (first-frame env) (car env))
(define (make-frame variables values)
  (cons variables values))
(define (frame-variables frame) (car frame))
(define (frame-values frame) (cdr frame))

请注意,与其他添加一个的不同,这会添加一整套。例如。要将x绑定到5,将y绑定到7

(variables '(x y)  '(5 7) env)

我将自己添加:

(define (variable var val env)
  (hash-set env var val))

(define (get-value name env)
  (hash-ref env name (lambda () (error "Unbound variable" name))))

;; the empty environment
(define the-empty-environment '#hasheq()) 

这个答案的全部意义在于,您可以选择get-valuevariablethe-empty-environment。只要你能够实现这些,它的确如何完成并不重要。你可以替换另一个,解释器仍然可以工作。除了要求你一次做一帧的SICP之外。