OZ Programming Top Two Max Numbers in A List

Cory_1 0 Tallied Votes 242 Views Share

I am having trouble with an OZ Program (Mozart Programming). I have a code to find the maximum element in a list.
The code I have so far is:

local
 fun {MaxList L1}
  case L1
  of nil then 0
  [] X|Xr then
   if {MaxList Xr}>X then {MaxList Xr}
    else X
   end
  end
 end
in
 {Browse {MaxList  [3 ~2 0 4 5 1]}}
end

This code will return '5', the max element
However, I am trying to write the code so that it will return the two largest elements in descending order. So first '5', then '4'.
I'm just not sure how to go about doing that. If this was Java, I would create another element Y, with a while loop saying that 'while Y < X (since X = 5)', then lines of code to do the operation. However, as far as I know, OZ does not support the while function. Any ideas? If anyone needs to install Oz for windows, use this website: http://osama-talaat.blogspot.com/2014/03/how-to-setup-mozart-as-oz-programming.html

local
 fun {MaxList L1}
  case L1
  of nil then 0
  [] X|Xr then
   if {MaxList Xr}>X then {MaxList Xr}
    else X
   end
  end
 end
in
 {Browse {MaxList  [3 ~2 0 4 5 1]}}
end