DrRacket编程
我在一份作业中得到了这一点:DrRacket编程,racket,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))
; 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")))
我知道我需要重复这两个列表,但不确定如何进行。这将起作用:
(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))))))))
这看起来像是来自EOPL,在这种情况下,这是本书开头的“手指练习”之一。