Scheme: change value of an element in a list
问题 I hate using SO as a way to find simple functions, but I really can't find a function like this anywhere: Given a list (1 2 3 4 5), I'd like the equivalent of (PHP's, Perl's, Python's) $a = array(1, 2, 3, 4, 5); $a[3] = 100; Which results in (1 2 3 100 5) Thanks! 回答1: You can write list-set! of Guile, like so: (define a (list 1 2 3 4)) ; a is '(1 2 3 4) (define (list-set! list k val) (if (zero? k) (set-car! list val) (list-set! (cdr list) (- k 1) val))) (list-set! a 2 100) ; a is '(1 2 100 4)