DrRacket编程

时间:2012-11-20 23:42:48

标签: racket

我在作业中得到了这个:

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

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

1 个答案:

答案 0 :(得分: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))))))))