In reply to code that I wrote that looked though a list of values and failed if two of them were equal (==), Raph suggested the code below--which is better than my original nested double loop.
 
In class today, Brian Smith proposed the following clever hack.
 
   proc {AllDistinct Xs}
      thread
         try {MakeRecord test Xs _}
         catch _ then fail end
      end
   end
 
It uses the fact that MakeRecord will fail if the field names it gets have a repeated value. It suspends until all values are instantiated. So the point is not really to make a record but to use MakeRecord's check on the list of fields to detect duplicates. The only reason to wrap it all in a try-catch is to get rid of the diagnostics that are printed in the Oz emulator buffer window otherwise.
 
-- Russ
 
On 10/25/05, Raphael Collet <[EMAIL PROTECTED]> wrote:
...
I also have a suggestion for the double loop in AllDistinct.  I tend to
avoid indexed loops, and prefer loops on lists instead.  You can write
AllDistinct as

proc {AllDistinct Xs}
   %% each element in Xs is different from its successors in Xs
   case Xs of X|Xr then
      for Y in Xr do
         thread X==Y = false end
      end
      {AllDistinct Xr}
   else skip end
end

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

Reply via email to