开发者

Scheme - how do I modify an individual element in a list?

开发者 https://www.devze.com 2022-12-12 01:45 出处:网络
If I have a 开发者_运维问答list of 0\'s, how would I modify, for example, the 16th 0 in the list?You have to write it yourself. It is not built in to Scheme because it\'s not idiomatic and it can be b

If I have a 开发者_运维问答list of 0's, how would I modify, for example, the 16th 0 in the list?


You have to write it yourself. It is not built in to Scheme because it's not idiomatic and it can be built easily from set-car!.

(define (list-set! l k obj)
  (cond
    ((or (< k 0) (null? l)) #f)
    ((= k 0) (set-car! l obj))
    (else (list-set! (cdr l) (- k 1) obj))))

If you are doing this a lot, you should probably look at using vectors and vector-set! instead.

0

精彩评论

暂无评论...
验证码 换一张
取 消