2016-01-28 53 views
0

我遇到的特殊問題是爲question 4.16b of Structure and Interpretation of Computer Programs創建解決方案。這裏的程序需要被創建,轉換擺脫名單上的外括號

(lambda (a b) 
    (define u 'u) 
    (define v 'v) 
    'e1)) 

分爲:

(lambda (a b) 
    (let ((u '*unassigned*) 
     (v '*unassigned*)) 
    (set! u 'u) 
    (set! v 'v) 
    'e1)) 

我的做法(見下文)並沒有這樣做,而是將其轉換成:

(lambda (a b) 
    (let ((u *unassigned*) 
     (v *unassigned*)) 
    ((set! u 'u) 
    (set! v 'v)) 
    ('e1))) 

這裏我們在(見下文)和其他機構(('e1)以上)生成的sets!列表中存在問題cons current-element rest-of-body(請參閱低)。它們被添加到列表中,而我想將它們作爲單個語句,即(set! u 'u) (set! v 'v)而不是((set! u 'u) (set! v 'v))'e1而不是`('e1)。

步驟:

;; b. Write a procedure scan-out-defines that takes a procedure body and returns an 
;; equivalent one that has no internal definitions, by making the transformation 
;; described above. 

(define (scan-out expr) 
    (let ((vars (cadr expr)) 
     (body (cddr expr))) 
    (make-lambda vars 
       ; loop over body, 
       ; store all definition names and bodies of the defines 
       ; once finished looping transform those into lets 
       ; where the rest is added to the body 
       (let body-transform ((body-elements body) 
             (definition-names '()) 
             (definition-bodies '()) 
             (rest-of-body '())) 
        (if (null? body-elements) 
        (transform-define-into-let definition-names 
               definition-bodies 
               rest-of-body) 
        (let ((current-element (car body-elements))) 
         (if (tagged-list? current-element 'define) 
         (body-transform (cdr body-elements) 
             (cons (get-definition-name current-element) 
               definition-names) 
             (cons (get-definition-body current-element) 
               definition-bodies) 
             rest-of-body) 
         (body-transform (cdr body-elements) 
             definition-names 
             definition-bodies 
             (cons current-element rest-of-body))))))))) 


(define (tagged-list? exp tag) 
    (if (pair? exp) 
    (eq? (car exp) tag) 
    false)) 

(define (get-definition-name expr) 
    (cadr expr)) 

(define (get-definition-body expr) 
    (caddr expr)) 

(define (transform-define-into-let vars vals rest-of-body) 
    (list (list 'let (make-unassigned-vars vars) 
     (make-sets vars vals) 
     rest-of-body))) 

(define (make-unassigned-vars vars) 
    (let aux ((var-elements vars) 
      (unassigned-vars '())) 
    (if (null? var-elements) 
     unassigned-vars 
     (aux (cdr var-elements) 
      (cons (list (car var-elements) '*unassigned*) unassigned-vars))))) 

(define (make-sets vars vals) 
    (let aux ((var-elements vars) 
      (val-elements vals) 
      (sets '())) 
    (if (null? var-elements) 
     sets 
     (aux (cdr var-elements) 
      (cdr val-elements) 
      (cons (list 'set! (car var-elements) (car val-elements)) sets))))) 

(define (make-lambda parameters body) 
    (cons 'lambda (cons parameters body))) 

; testing 
(scan-out '(lambda (a b) 
      (define u 'u) 
      (define v 'v) 
      'e1)) 

; Should be transformed into: 

; => (lambda (a b) 
;  (let ((u '*unassigned*) 
;   (v '*unassigned*)) 
;  (set! u 'u) 
;  (set! v 'v) 
;  'e1)) 

; But is transformed into: 

; => (lambda (a b) 
;  (let ((u *unassigned*) 
;   (v *unassigned*)) 
;  ((set! u (quote u)) 
;   (set! v (quote v))) 
;  ((quote e1)))) 

我試過被壓扁,像這樣的列表:

(define (transform-define-into-let definition-names definition-bodies rest-of-body) 
    (list (list 'let (make-unassigned-vars definition-names) 
     (append* (make-sets definition-names definition-bodies)) 
     (append* rest-of-body)))) 

但當時只有rest-of-body被剝離外括號,make-sets仍然是一個列表:例如,

(lambda (a b) 
    (let ((u *unassigned*) 
     (v *unassigned*)) 
    ((set! u 'u) 
    (set! v 'v)) 
    'e1)) 

什麼是正確的擺脫方式o f外部括號?

如果有人能幫助我,這將不勝感激。

回答

1

你應該改變:

(define (transform-define-into-let vars vals rest-of-body) 
    (list (list 'let (make-unassigned-vars vars) 
     (make-sets vars vals) 
     rest-of-body))) 

到:

(define (transform-define-into-let vars vals rest-of-body) 
    (list (append (list 'let (make-unassigned-vars vars)) 
       (append (make-sets vars vals) 
         rest-of-body)))) 

也:

(define (make-unassigned-vars vars) 
    (let aux ((var-elements vars) 
      (unassigned-vars '())) 
    (if (null? var-elements) 
     unassigned-vars 
     (aux (cdr var-elements) 
      (cons (list (car var-elements) '*unassigned*) unassigned-vars))))) 

(define (make-unassigned-vars vars) 
    (let aux ((var-elements vars) 
      (unassigned-vars '())) 
    (if (null? var-elements) 
     unassigned-vars 
     (aux (cdr var-elements) 
      (cons (list (car var-elements) ''*unassigned*) unassigned-vars))))) 

最後注意'u(quote u)相同。

+0

嗨Renzo!非常感謝!!! :D只改變'transform-define-into-let'就足夠了。注意'make-unassigned-vars'在你的答案中沒有改變。 ;) –

+0

不客氣!請注意,'* unassigned *'之前有兩個單引號(''),否則您生成'(u * unassigned *)'而不是'(u'* unassigned *)''。 – Renzo

+0

啊我現在看到了。下一次,我會用一個程序來代替我的眼睛,然後再說一些沒有改變的東西。顯然這個引用是一個身份運算符,它對自身進行評估(一次)([source](http://www.phyast.pitt.edu/~micheles/scheme/scheme8.html))。我沒有看到這就是爲什麼''''不會評價爲'a'(兩次認證)。 –