1

我在作业中得到了这个:

; 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")))

我知道我需要重复这两个列表,但不确定如何去做。

4

1 回答 1

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))))))))
于 2013-03-25T20:07:18.703 回答