方案:更改列表中元素的值 [英] Scheme: change value of an element in a list
本文介绍了方案:更改列表中元素的值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我讨厌使用SO作为查找简单函数的方法,但是我真的在任何地方都找不到这样的函数:
I hate using SO as a way to find simple functions, but I really can't find a function like this anywhere:
给出一个列表(1 2 3 4 5),我想要相当于(PHP,Perl,Python)
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;
得出(1 2 3 100 5)
Which results in (1 2 3 100 5)
谢谢!
推荐答案
您可以编写Guile的list-set!
,如下所示:
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)
(在DrRacket中进行了尝试.)
(Tried this in DrRacket.)
这篇关于方案:更改列表中元素的值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文