2012-11-20 24 views
0

我得到這個在分配:DrRacket編程

; subst: [Listof Value] [Listof Name] SExp -> SExp 
; substitute the corresponding value for each listed variable name in s 
; leave all other names in s unmodified 

(define (subst vals vars s) ...) 

(define s1 '(foo a 29 (bar "z" b))) 
(check-expect (subst '(3 "x") '(a b) s1) '(foo 3 29 (bar "z" "x"))) 

我知道我需要復發下來兩份名單,但我不知道該如何去做。

+0

這看上去就像EOPL的,在這種情況下,它的「手指操」一個在書的開頭。 – dyoo

回答

0

這將工作:

(define (subst vals vars s) 
    (let ((mapping (map vars vals))) 
    (let subbing ((s s)) 
     (if (null? s)` 
      '() 
      (let ((s1 (car s))) 
      (cons (cond ((and (symbol? s1) (assoc s1 mapping)) => cdr) 
         ((list? s1) (subbing s1)) 
         (else s1)) 
        (subbing (cdr s))))))))