ozmozart

Make a decreasing list from a number


I need to create a decreasing list of all whole numbers between X and Y (inclusive).

I am trying this:

declare
local
   fun {Dec From To}
      From | {Dec From-1 To}
   end
in
   {Browse {Loop 8 1}} % Should Display: [8, 7, 6, 5, 4, 3, 2, 1]
end

The Mozart compiler shows that the code is accepted, but no Browse window opens up.

What is wrong?


Solution

  • I think nothing is shown because your function Dec is an infinite loop because you never set when to stop, and that's why the browse window is not displayed.

    The function should be:

    fun {Dec From To}
      if From == To then 
        nil
      else
        From | {Dec (From-1) To}
      end
    end 
    

    I hope this was helpful :)