How to replace an item by another in a list in DrScheme when given paramters are two items and a list?

后端 未结 4 571
轻奢々
轻奢々 2021-01-21 18:42

How to replace an item by another in a list in DrScheme when given paramters are two items and a list?

4条回答
  •  耶瑟儿~
    2021-01-21 19:35

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

提交回复
热议问题