0

Let's say we have the following code:

local L in
    L = {List.make 10 $}
    % Bind nth element to something here
end

How does one set any of these unbound values? The Oz List Documentation didn't shed any light on this. The only related question I found was: How do you change an element in a list in Oz? where the answer didn't compile for me, nor did I find how I could get it to compile.

Community
  • 1
  • 1
Spyral
  • 760
  • 1
  • 12
  • 33

2 Answers2

0

I actually was able to use something from the Oz List Documentation to get it to work!

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
$}
Spyral
  • 760
  • 1
  • 12
  • 33
  • 1
    Alternatively, you can also just do `{Nth L Index} = Value`. – wmeyer Apr 06 '15 at 14:49
  • @wmeyer What the hell! :p I tried this but it failed, that's why I eventually went with this crazy implementation.. Must have made a mistake somewhere else in the code.. THANKS FOR THE TIP :D! – Spyral Apr 06 '15 at 16:11
0

wmeyer is right, the complete implementation could be for example:

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

You'll get the correct output [_ _ _ _ 1 _ _ _ _ _]. You can also access en element directly, but is not a programmatically solution, as you may know every list is a cons (head|tail), so you can access the first element with L.1 the second with L.2.1 and so on.. Last thing, you have no need to put "$" in the second line as you are already assigning the result of the List.make to L.

rok
  • 2,574
  • 3
  • 23
  • 44