Filip Konvicka wrote:
Some time ago, I have created a simple Sudoku script for my co-workers to show them Oz basics. It's just a straightforward implementation (and maybe a bit clumsy).

Your solution is pretty hard to read. I have tried to simplify it a bit, see the attachment.

Cheers,
raph
local
   fun {Script Spec}
      proc {$ Root}
         %% tuples are more efficient than lists
         Root={List.make 9}
         for Row in Root do Row={FD.list 9 1#9} end

         %% All numbers in each row are distinct
         {ForAll Root FD.distinct}

         %% All numbers in each column are distinct
         for I in 1..9 do
            {FD.distinct {Map Root fun {$ Row} {Nth Row I} end}}
         end

         %% All numbers in each 3x3 square are distinct
         for I in 0..2 do
            RowOfSquares={List.take {List.drop Root I*3} 3}
         in
            for J in 0..2 do
               Elems = for Row in RowOfSquares  append:App do
                          {App {List.take {List.drop Row J*3} 3}}
                       end
            in
               {FD.distinct Elems}
            end
         end

         %% constrain Root with Spec; simply unify Root with a clone
         %% of Spec
         Root = {Map Spec
                 fun {$ Row}
                    {Map Row
                     fun {$ X} if {IsDet X} then X else _ end end}
                 end}

         %% Search...
         {FD.distribute ff {FoldR Root Append nil}}
      end
   end
in
   {Show {SearchOne
          {Script [[_ 9 _ 3 _ _ _ 1 _]
                   [2 _ 3 _ _ 1 _ _ _]
                   [_ _ _ _ 7 5 _ 8 _]
                   [_ _ 2 _ _ _ _ 5 6]
                   [_ _ _ 7 _ 8 _ _ _]
                   [7 1 _ _ _ _ 2 _ _]
                   [_ 2 _ 4 9 _ _ _ _]
                   [_ _ _ 8 _ _ 5 _ 1]
                   [_ 7 _ _ _ 2 _ 9 _]]}}}
end
_________________________________________________________________________________
mozart-users mailing list                               
[email protected]
http://www.mozart-oz.org/mailman/listinfo/mozart-users

Reply via email to