On Fri, Feb 13, 2009 at 12:18 PM, Quirino Zagarese <
[email protected]> wrote:

> Hi,
> I'm new to Oz and I'm trying to write a simple function which take two
> arguments, a list and a boolean,
> and switches each couple of elements if they are not in ascending order. If
> no switch takes place, the function
> must return the initial_list#true otherwise the new_list#false.
> Here is my function:
>
> declare
> fun{SwitchAndTest Ls Bool}
>    case Ls of nil then nil#Bool
>    [] [X] then  X|nil#Bool
>    [] X1|X2|T then
>       if X1>X2 then  X2|{SwitchAndTest (X1|T) false}
>       else
>           X1|{SwitchAndTest (X2|T) Bool}
>       end
>    end
> end
>
> Running  {Browse {SwitchAndTest [1 3 2 4 5 6 8 7] true}}  returns
> 1|2|3|4|5|6|7|8|nil#false
> instead of [1 2 3 4 5 6 7 8]#false.
> It's a newbie question but I couldn't find any solution for now.
> Then is there a way to match such an expression in a case statement? I mean
> is there a way to write something like
>
> case {SwitchAndTest Ls true} of ANYTHING#false   so that it will match
> 1|2|3|4|5|6|7|8|nil#false ?
> Thanks in advance
>

Split the function in two: one that takes care of the toplevel result
(list#boolean), and another one that builds the list and determines the
boolean.  The second function actually has two outputs: the list and the
boolean.

fun {Switch L}
   B in {SwitchAndTest L B}#B
end

fun {SwitchAndTest L ?B}
   case L of nil then B=true   nil
   [] [X] then B=true   [X]
   [] X1|X2|T then
      if X1>X2 then
         B=false   X2|{SwitchAndTest X1|T _}
      else
         X1|{SwitchAndTest X2|T B}
      end
   end
end

Cheers,
raph
_________________________________________________________________________________
mozart-users mailing list                               
[email protected]
http://www.mozart-oz.org/mailman/listinfo/mozart-users

Reply via email to