2011-01-21 12 views

答えて

5

mapには、引数が置換する項目と等しい場合は置換項目を返し、それ以外の場合は引数を返します。

0
; replace first occurrence of b with a in list ls, q? is used for comparison 
(define (replace q? a b ls) 
    (cond ((null? ls) '()) 
    ((q? (car ls) b) (cons a (cdr ls))) 
    (else (cons (car ls) (replace a b (cdr ls)))))) 
0
; replace first occurrence of b with a in list ls, q? is used for comparison 
(define (replace q? a b ls) 
    (cond ((null? ls) '()) 
     ((q? (car ls) b) (replace q? a b (cons a (cdr ls)))) 
     (else (cons (car ls) (replace a b (cdr ls)))))) 
0

これはDrRacket

(define (subst fsym tsym lst) 

(cond 

[(null? lst) lst] 

[eq? (first lst) fsym)(cons tsym (subst fsym tsym (rest lst)))] 

[else (cons (first lst)(subst fsym tsym (rest lst)))])) 

(subst 'a 'b '(f g a h a)) 

;the results will be as follows. 

'(f g b h b) 
におけるシンボルtsymにリストLST内fsymの出現をすべて置き換えます
関連する問題