Courses/CS 460/Fall 2005/Homework/Oscar Chen/Oct 22
From CSWiki
< Courses | CS 460 | Fall 2005 | Homework | Oscar Chen
[edit] Oct 22
- Two Different Attempts at Judy's January 2000 Puzzle
- URL: [1]
- Abbott style now works... in a rough sort of way.
[edit] Abbott Style
Revised, with restrictions. Is there a better way to do it.
% January 2000 Masquerade Puzzle
local
Suspects = ['Mr. Green' 'Miss Scarlet' 'Mrs. Peacock' 'Mrs. White' 'Prof. Plum']
Cars = [blue green purple red white]
Roles = [butler chauffer cook maid gardner]
proc {Append Xs Ys Zs}
choice
Xs = nil
Ys = Zs
[]
Xr X in
Xs = X | Xr
Zs = X | {Append Xr Ys}
end
end
proc {IsASuspect ?Suspect} {IsAMember Suspect Suspects} end
proc {IsACar ?Car} {IsAMember Car Cars} end
proc {IsARole ?Role} {IsAMember Role Roles} end
proc {IsAMember X Y}
H Tail in
Y = H | Tail
choice H = X
[]
{IsAMember X Tail} end
end
proc {IsAProperty PPs ?Property ?Value}
{IsAMember Property#Value {Record.toListInd PPs}}
end
%Prof. Plum didn't drive the red car.
proc {Clue1 PPs}
Car in
{IsAProperty PPs Car 'Prof. Plum'}
{IsACar Car}
Car == red = false
end
%Mr. Green played the part of the cook.
proc {Clue2 PPs}
{IsAProperty PPs cook 'Mr. Green'}
end
%Mrs. Peacock, who arrived in the green car, did not masquerade
%as the gardener.
proc {Clue3 PPs}
Role in
{IsAProperty PPs green 'Mrs. Peacock'}
{IsAProperty PPs Role 'Mrs. Peacock'}
{IsARole Role}
Role == gardner = false
end
%Mrs. White (who didn't drive a purple car) impersonated the
%chauffeur.
proc {Clue4 PPs}
Car in
{IsAProperty PPs Car 'Mrs. White'}
{IsACar Car}
Car == purple = false
{IsAProperty PPs chauffer 'Mrs. White'}
end
%The person who arrived in the white car (who wasn't Prof. Plum)
%played the part of the butler to perfection.
proc {Clue5 PPs}
Suspect in
{IsAProperty PPs white Suspect}
{IsAProperty PPs butler Suspect}
{IsASuspect Suspect}
PPs.butler == 'Mrs. White' = false
PPs.butler == 'Mr. Green' = false
PPs.white == 'Mrs. Peacock' = false
Suspect == 'Prof. Plum' = false
end
proc {Clue6 PPs}
Suspect1 Suspect2 Suspect3 in
{IsAProperty PPs red Suspect1}
{IsAProperty PPs blue Suspect2}
{IsAProperty PPs purple Suspect3}
{IsASuspect Suspect1}
{IsASuspect Suspect2}
{IsASuspect Suspect3}
PPs.red == 'Mrs. Peacock' = false
PPs.red == 'Miss Scarlet' = false
PPs.blue == 'Mrs. Peacock' = false
PPs.purple == 'Mrs. Peacock' = false
PPs.purple == 'Prof. Plum' = false
PPs.blue == PPs.red = false
PPs.blue == PPs.purple = false
PPs.red == PPs.white = false
PPs.purple == PPs.red = false
PPs.purple == PPs.white = false
end
proc {Clue7 PPs}
Suspect1 in
{IsAProperty PPs gardner Suspect1}
{IsASuspect Suspect1}
PPs.gardner == 'Mrs. White' = false
PPs.gardner == 'Mr. Green' = false
PPs.gardner == PPs.maid = false
PPs.gardner == PPs.cook = false
PPs.gardner == PPs.chauffer = false
PPs.gardner == PPs.butler = false
end
fun {SolveLogicPuzzle}
Properties = {FoldR [Cars Roles] Append nil}
PPs = {MakeRecord puzzleProperties Properties}
in
{Clue1 PPs}
{Clue2 PPs}
{Clue3 PPs}
{Clue4 PPs}
{Clue5 PPs}
{Clue6 PPs}
{Clue7 PPs}
PPs
end
in
{Browse {SearchAll SolveLogicPuzzle}}
end
- Solution:
- Mr. Green, purple, cook
- Mrs. Peacock, green, downstairs maid
- Mrs. White, red, chauffeur
- Miss Scarlett, white, butler
- Prof. Plum, blue, gardener
[edit] Cynthia Style
% January 2000 Masquerade Puzzle
local
proc {Append Xs Ys Zs}
choice
Xs = nil
Ys = Zs
[]
Xr X in
Xs = X | Xr
Zs = X | {Append Xr Ys}
end
end
proc {IsAMember X Y}
H Tail in
Y = H | Tail
choice H = X
[]
{IsAMember X Tail} end
end
proc {IsASolution Property X}
{IsAMember {Solution Property} X}
end
proc {IsNotASolution Property X}
{!IsAMember {Solution Property} X}
end
proc {MakeSolution X}
{Record.make solution [suspect role car] X}
end
proc {Solution Properties X}
{MakeSolution X}
{ForAll Properties
proc {$ Property#Value}
X.Property = Value
end
}
end
proc {LogicSolution X}
X = [{Solution [suspect#'Mr. Green']}
{Solution [suspect#'Prof. Plum']}
{Solution [suspect#'Miss Scarlet']}
{Solution [suspect#'Mrs. Peacock']}
{Solution [suspect#'Mrs. White']}]
in
{IsNotASolution [suspect#'Prof. Plum' car#red] X}
{IsASolution [suspect#'Mr. Green' role#cook] X}
{IsASolution [suspect#'Mrs. Peacock' car#green] X}
{IsNotASolution [suspect#'Mrs. Peacock' role#gardner] X}
{IsNotASolution [car#green role#gardner] X}
{IsNotASolution [suspect#'Mrs. White' car#purple] X}
{IsASolution [suspect#'Mrs. White' role#chauffer] X}
{IsNotASolution [suspect#'Prof. Plum' car#white] X}
{IsASolution [car#white role#butler] X}
end
in
{Browse {SearchAll LogicSolution}}
end

