2014-04-20 35 views

回答

2

你的问题有些不明确。你想如何决定哪个项目被改变?

我假设你想用索引来完成它, “第0个子列表的第0项”。那就是:

to-report replace-subitem [index1 index2 lists value] 
    let old-sublist item index1 lists 
    report replace-item index1 lists (replace-item index2 old-sublist value) 
end 

observer> show replace-subitem 0 0 [[1 2] [3 4] [5 1]] 99 
observer: [[99 2] [3 4] [5 1]] 

你也可以想象根据其他标准进行更换。例如,假设我们希望在一个子表的第一项改变所有的1的出现到99那么这就是:

to-report replace-first [old new the-list] 
    if first the-list = old 
    [ report replace-item 0 the-list new ] 
    report the-list 
end 

to-report replace-firsts [old new lists] 
    report map [replace-first old new ?] lists 
end 

observer> show replace-firsts 1 99 [[1 2] [3 4] [1 1]] 
observer: [[99 2] [3 4] [99 1]] 

很多其他的答案是可能的,这取决于到底是什么问题,你正在努力解决。