7

I'm trying to create a list in Oz using the following code:

local Loop10 Xs in
   proc {Loop10 I}
      Xs={List.append Xs I}
      if I == 10 then skip
      else
     {Browse I}
     {Loop10 I+1}
      end
      {Browse Xs}
   end
{Loop10 0}
end

The Mozart compiler shows that the code is accepted, but no Browse window opens up. All I'm trying to do is create a list in Oz.

What's wrong with the code?

The Unfun Cat
  • 29,987
  • 31
  • 114
  • 156
el diablo
  • 2,647
  • 2
  • 20
  • 22

2 Answers2

8

Not sure that that's what you want, but to create a list of all whole numbers between X and Y (inclusive) you could do:

local
   fun {Loop From To}
      if From > To
      then nil
      else From | {Loop From+1 To}
      end
   end
in
   {Browse {Loop 0 10}} % Displays: [0,1,2,3,4,5,6,7,8,9,10]
end
sepp2k
  • 363,768
  • 54
  • 674
  • 675
  • Thanks. I actually stumbled upon the same method myself. Although in order to return a proper list the second argument to Loop must be To|nil so that the list is always terminated with a nil. thanks. – el diablo Sep 27 '09 at 12:00
  • 1
    In my opinion, it's more secure and more logical to do `if From < To then nil` because you can give a "To" less than "From" and get nil (and not an infinite list like in the above program) – yakoudbz Jul 30 '14 at 21:56
  • @yakoudbz Good point (except that it should be `>`, not `<`). Done. – sepp2k Jul 30 '14 at 23:32
5

Also the reason why you don't get any browser window is because the evaluation thread suspend due to this line:

Xs={List.append Xs I}

As it was previously mentioned a variable can only be assigned once but there is something else that is wrong with this line. You attempt to append Xs to I but Xs is still unbound. The thread thus suspends until a value has been assign to Xs.

Enter this interactively:

declare Xs in
{Browse {List.append Xs [2 3 4]}}

As you see nothing happens, no browser opens. Now enter this:

Xs= [1]

Since Xs becomes bound it unlocks the first evaluation "thread" and the browser will pop-up.

P.S. Sorry for the late answer, I just got interested in Oz :P

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
JFT
  • 827
  • 8
  • 6