0

I have to make a program in Oz that will return the max integer in a list. The code that I have so far looks like this:

    declare
    proc  {Max Xs K}
       case Xs
       of nil then K = 0
       [] X|Xr then
          local M in
              if M < X then M = X end
              {Max Xr K}
              K = M
          end
       end
    end

The Mozart environment will accept the code but won't return an answer. The input looks like this: {Browse {Max [1 2]}}. What am I doing wrong?

april
  • 1
  • 2
  • What do you mean by "Emacs will accept the code?" What does this question have to do with Emacs? – Drew Oct 11 '15 at 01:21

1 Answers1

0

You don't have any else clause, so how can you compare M and X ? M has no value at the beginning. I would also use function to make it simpler, you are btw not far from the solution:

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 [1 2 3 4 3]}}
end

Or you can do in more complicated but concise way,as suggested in rosetta code:

declare
  fun {Maximum X|Xr}         %% pattern-match on argument to make sure the list is not empty
     {FoldL Xr Value.max X}  %% fold the binary function Value.max over the list
  end
in
  {Show {Maximum [1 2 3 4 3]}} 
rok
  • 2,574
  • 3
  • 23
  • 44