列表綁定未綁定的值,讓我們說,我們有下面的代碼:如何在奧茲
local L in
L = {List.make 10 $}
% Bind nth element to something here
end
即如何設置這些綁定值嗎? Oz List Documentation沒有說明這一點。我發現的唯一相關問題是:How do you change an element in a list in Oz?答案沒有爲我編譯,我也沒有找到如何讓它編譯。
列表綁定未綁定的值,讓我們說,我們有下面的代碼:如何在奧茲
local L in
L = {List.make 10 $}
% Bind nth element to something here
end
即如何設置這些綁定值嗎? Oz List Documentation沒有說明這一點。我發現的唯一相關問題是:How do you change an element in a list in Oz?答案沒有爲我編譯,我也沒有找到如何讓它編譯。
我實際上可以使用Oz List Documentation中的某些東西來使其工作!
declare Temp Index Value L in
L = {List.make 10 $}
Index = %Index to be bound
Value = %Value for bound
Temp = {List.mapInd L
fun {$ I B}
if I == Index then
B = Value
else
B
end
end
$}
wmeyer是正確的,完整的實現可以是例如:
declare
local L I V in
L = {List.make 10}
I=5 %index to be bound
V=1 %value for bound
{List.nth L I}=V
{Browse L}
end
你會得到正確的輸出[_ _ _ _ 1 _ _ _ _ _]
。您也可以直接訪問en元素,但不是一個以編程方式的解決方案,因爲您可能知道每個列表都是cons(頭尾),因此您可以使用L.1訪問第一個元素,第二個元素使用L.2.1,等等.. 最後一件事,因爲您已經將List.make的結果分配給了L,所以不需要在第二行輸入「$」。
或者,您也可以只執行「{Nth L Index} =值'。 – wmeyer 2015-04-06 14:49:19
@wmeyer什麼鬼! :p我嘗試過但是失敗了,這就是爲什麼我最終與這個瘋狂的實現一起使用的原因。一定是在代碼中的其他地方犯了一個錯誤。謝謝提示:D! – Spyral 2015-04-06 16:11:17