Courses/CS 460/Fall 2005/Homework/Joey Leung/Oct 29

From CSWiki

Jump to: navigation, search

Contents

[edit] Solve 3 Puzzles

[edit] Flower Boxes

Do not work, I get nil for the result.


declare

   proc {AllInstantiated Xs Values}
      {ForAll Xs proc {$ X} {IsIn X Values} end}
   end

   proc {Append ?Xs ?Ys ?Zs}
      choice
         Xs = nil 
         Ys = Zs
      [] X Xr in
         Xs = X | Xr
         Zs = X | {Append Xr Ys}
      end
   end

   fun {GetFields Field Elts}
      {Map Elts fun {$ Elt} Elt.Field end}
   end

   fun {IndexOf X Xs}
      fun {IndexOf3 X Xs N}
         case Xs of
            nil then fail
         [] Y | Rest then
            if X == Y then N else {IndexOf3 X Rest N+1} end
         end
      end
   in
     {IndexOf3 X Xs 1}
   end

   proc {IsIn ?X Xs} {Append _ X|_ Xs} end
   proc {IsAnElt ?X Elts} {IsIn X {Record.toList Elts}} end

   proc {AllDistinct List}
      L = {Length List} in
      for I in 1; I =< L-1; I+1 do
         for J in I+1; J =< L; J+1 do
            {NotEqual {Nth List I} {Nth List J}}
         end
      end
   end

   proc {NotEqual X Y} thread X == Y = false end end

   proc {Precedes ?X1 ?X2 Xs}
      thread {IndexOf X1 Xs} < {IndexOf X2 Xs} = true end
   end

   fun {Match R1 R2}
      {Record.all
       {Record.zip R1 R2 fun {$ A B} A == B end}
       fun {$ X} X == true end}
   end

   proc {Propagate Condition Result R}
      thread if {Match Condition R} then {UnifyRecs Result R} end end    
   end

   proc {PropagateAll Condition Result Rec}
      {Record.forAll Rec proc {$ R} {Propagate Condition Result R} end}
   end

   proc {UnifyRecs R1 R2}
      {Record.zip R1 R2 fun {$ A B} A = B end _}
   end

local

FirstNames = [julie kate lola mary]   
LastNames = [drake evans florez gumble]
      
   proc {Clue1 SPs}
      {NotEqual SPs.daisies.lastName drake}
      {NotEqual SPs.daisies.lastName evans}
      {NotEqual SPs.daisies.firstName julie}
   end

   proc {Clue2 SPs}
	{NotEqual SPs.violets.firstName kate}
   end

   proc {Clue3 SPs}
      {NotEqual SPs.marigolds.lastName drake}
   end
   
   proc {Clue4 SPs}
      {NotEqual SPs.violets.lastName florez}
      %{NotInRecord person(lastName:florez firtstname:lola) SPs}
   end
   
   proc {Clue5 SPs}
      {NotEqual SPs.marigolds.flower julie}
  end

   proc {Clue6 SPs}
       {NotEqual SPs.daisies.firstname kate}
       %{NotInRecord person(lastName:evans firtstname:kate) SPs}
   end    
    
   fun {FlowerBoxes}
      SPs = {MakeRecord flowers [daisies marigolds petunias violets]}
      Ms
      Ds
      FlowerList = {Record.toList SPs}
   in
      {Record.forAll SPs proc {$ S} S = {Record.make properties [firstname lastname]} end}

      Ms = {GetFields firstname SPs}
      {AllDistinct Ms}
      Ds = {GetFields lastname SPs}
      {AllDistinct Ds}
      
      {Clue1 SPs}
      {Clue2 SPs}
      {Clue3 SPs}
      {Clue4 SPs}
      {Clue5 SPs}
      {Clue6 SPs}
      
       {AllInstantiated Ms firstname}
       {AllInstantiated Ds lastname}
       SPs
      end
in
   {Browse {SearchAll FlowerBoxes}}
end


[edit] 2





[edit] 3