2014-04-20 38 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]] 

很多其他的答案是可能的,這取決於到底是什麼問題,你正在努力解決。