Courses/CS 460/Fall 2005/Homework/Xuong Tsan/Oct 22
From CSWiki
Contents |
[edit] Homework Page
Homework page is: | http://cs.calstatela.edu/~wiki/index.php/Courses/CS_460/Fall_2005/Homework/Xuong_Tsan
[edit] Feeding Time
Zookeeper George was in charge of feeding all of the animals in the morning. He had a regular schedule that he followed every day. Can you figure it out from the clues?
1. The giraffes were fed before the zebras but after the monkeys.
2. The bears were fed 15 minutes after the monkeys.
3. The lions were fed after the zebras.
% Feeding Time
local
Animals = ['Bears' 'Giraffes' 'Lions' 'Monkeys' 'Zebras']
Times = ['6:30 AM' '6:45 AM' '7:00 AM' '7:15 AM' '7:30 AM']
% If Zs is not instantiated (or at least not bounded, then
% Xs and Ys should be or the number of solutions will grow
% without limit.
proc {Append ?Xs ?Ys ?Zs}
choice
Xs = nil
Ys = Zs
[] Head XRest ZRest in
Xs = Head|XRest
Zs = Head|ZRest
{Append XRest Ys ZRest}
end
end
proc {Before2 ?Time1 ?Time2}
Rest in
{IsATail Times Time1|Rest}
{IsAMember Time2 Rest}
end
% Value is the value of field Property in the record TPs
% Both Property and Value may be uninstantiated
proc {IsAPropertyValue TPs ?Property ?Value}
{IsAMember Property#Value {Record.toListInd TPs}}
end
% Name is a given name.
proc {IsAnimals ?Name} {IsAMember Name Animals} end
% Color is a car's color
proc {IsATimes ?Time} {IsAMember Time Times} end
% Look at this neat definition for IsAMember/2.
% X is a member of Xs.
proc {IsAMember ?X Xs} {Append _ X|_ Xs} end
% Tail is a tail of the list Xs
proc {IsATail ?Xs ?Tail} {Append _ Tail Xs} end
proc {Clue1 FTs}
Time Time1 Time2 Time3
in
%Giraffes were fed before zebras but after monkeys
{IsAPropertyValue FTs Time 'Giraffes'}
{IsAPropertyValue FTs Time1 'Zebras'}
{Before2 Time Time1}
{IsAPropertyValue FTs Time2 'Monkeys'}
{IsAPropertyValue FTs Time3 'Giraffes'}
{Before2 Time2 Time3}
end
proc {Clue2 FTs}
Time1 Time2 Time3 Time4 in
%The bears were fed 15 minutes after the monkeys
{IsAPropertyValue FTs Time1 'Monkeys'}
{IsAPropertyValue FTs Time2 'Bears'}
{Before2 Time1 Time2}
%by implication bears were fed before Giraffes
{IsAPropertyValue FTs Time3 'Bears'}
{IsAPropertyValue FTs Time4 'Giraffes'}
{Before2 Time3 Time4}
end
proc {Clue3 FTs}
Time1 Time2 in
%The lions were fed after the zebras
{IsAPropertyValue FTs Time1 'Zebras'}
{IsAPropertyValue FTs Time2 'Lions'}
{Before2 Time1 Time2}
end
fun {FeedingTime}
Properties = {FoldR [Times] Append nil}
FTs = {MakeRecord feeding_Time Properties}
in
% To debug, comment out all the clues except the one
% you are working on and the ones that already work.
{Clue1 FTs}
{Clue2 FTs}
{Clue3 FTs}
FTs
end
in
{Browse {SearchAll FeedingTime}}
end
[edit] Masquerading for the Gentry
Mr. Boddy has invited the lovely (and wealthy) Lady Melon to dine, and now all he has to do is set the stage. It appears that Mr. Boddy has made some very unwise investment decisions and as a result he has had to let his extensive personal staff go. However, after much wheedling and a bit of blackmail, he persuaded five of the six suspects in the game of CLUE to help him out (Col. Mustard could not be reached as he was on Safari in Kenya). Master impersonators, the five suspects - Mr. Green, Miss Scarlet, Mrs. Peacock, Mrs. White, and Prof. Plum, slipped inconspicuously into the roles of butler, chauffeur, cook, downstairs maid, and gardener, with each suspect playing a different role. Each masquerader arrived at the Boddy mansion driving a car of a different color - either blue, green, purple, red, or white, and no suspect drove a car associated with his or her name. Note: Miss Scarlet's name is associated with the color red, Mrs. Peacock with blue, and Prof. Plum with purple.
Can you determine, for each suspect, what color of car he or she drove, and which member of the staff he or she impersonated?
1. Prof. Plum didn't drive the red car.
2. Mr. Green played the part of the cook.
3. Mrs. Peacock, who arrived in the green car, did not masquerade as the gardener.
4. Mrs. White (who didn't drive a purple car) impersonated the chauffeur.
5. The person who arrived in the white car (who wasn't Prof. Plum) played the part of the butler to perfection.
% Masquerading for the Gentry Puzzle
local
GivenNames = ['Mr. Green' 'Mrs. Peacock' 'Mrs. White' 'Miss Scarlett' 'Prof. Plum']
CarColors = [red white blue green purple]
Roles = [cook downstairs_maid chauffeur butler gardener]
% If Zs is not instantiated (or at least not bounded, then
% Xs and Ys should be or the number of solutions will grow
% without limit.
proc {Append ?Xs ?Ys ?Zs}
choice
Xs = nil
Ys = Zs
[] Head XRest ZRest in
Xs = Head|XRest
Zs = Head|ZRest
{Append XRest Ys ZRest}
end
end
% Value is the value of field Property in the record TPs
% Both Property and Value may be uninstantiated
proc {IsAPropertyValue TPs ?Property ?Value}
{IsAMember Property#Value {Record.toListInd TPs}}
end
% Name is a given name.
proc {IsAGivenName ?Name} {IsAMember Name GivenNames} end
% Color is a car's color
proc {IsACarColor ?Color} {IsAMember Color CarColors} end
% Role is the Roles
proc {IsARole ?PlayRole} {IsAMember PlayRole Roles} end
% Look at this neat definition for IsAMember/2.
% X is a member of Xs.
proc {IsAMember ?X Xs} {Append _ X|_ Xs} end
proc {Clue1 MPs}
Color in
% Prof. Plum didn't dirve the red car
{IsAPropertyValue MPs Color 'Prof. Plum'}
%Ensure Color1 is a Car's Color
{IsACarColor Color}
Color == red = false
end
proc {Clue2 MPs}
% Mr. Green played the part of the cook
{IsAPropertyValue MPs cook 'Mr. Green'}
end
proc {Clue3 MPs}
Role in
%Mrs. Peacock arrived in green car
{IsAPropertyValue MPs green 'Mrs. Peacock'}
%Mrs. Peacock didn't masquerade as the gardener
{IsAPropertyValue MPs Role 'Mrs. Peacock'}
{IsARole Role}
Role == gardener = false
end
proc {Clue4 MPs}
Color1 in
%Mrs. White didn't drive purple car
{IsAPropertyValue MPs Color1 'Mrs. White'}
{IsACarColor Color1}
Color1 == purple = false
%Mrs. White impersonated the chauffeur
{IsAPropertyValue MPs chauffeur 'Mrs. White'}
end
proc {Clue5 MPs}
Name Role1 Role2 Color2 in
%Person who arrived in the white car wasn't Prof. Plum
{IsAPropertyValue MPs white Name}
{IsAGivenName Name}
Name == 'Prof. Plum' = false
%butler drive white car
%(by implication, Mrs. Peacock is not butler
{IsAPropertyValue MPs Role1 'Mrs. Peacock'}
{IsARole Role1}
Role1 == butler = false
%(by implication, Prof. Plum is not butler
{IsAPropertyValue MPs Role2 'Prof. Plum'}
{IsARole Role2}
Role2 == butler = false
%by elimination Prof. Plum is gardener
{IsAPropertyValue MPs gardener 'Prof. Plum'}
%by elimination Miss Scarlett is butler
{IsAPropertyValue MPs butler 'Miss Scarlett'}
%and butler drive a white car
{IsAPropertyValue MPs white 'Miss Scarlett'}
%by elimation Mrs. White drives red car
{IsAPropertyValue MPs red 'Mrs. White'}
%by implication Prof. Plum drives blue car
{IsAPropertyValue MPs blue 'Prof. Plum'}
%by elimination Mr. Green drive purple car
{IsAPropertyValue MPs Color2 'Mr. Green'}
{IsACarColor Color2}
Color2 == blue = false
end
fun {Masquerading}
Properties = {FoldR [CarColors Roles] Append nil}
MPs = {MakeRecord masqueradeParty Properties}
in
% To debug, comment out all the clues except the one
% you are working on and the ones that already work.
{Clue1 MPs}
{Clue2 MPs}
{Clue3 MPs}
{Clue4 MPs}
{Clue5 MPs}
MPs
end
in
{Browse {SearchAll Masquerading}}
end

