Courses/CS 460/Fall 2005/Homework/Kelly Breed/Oct 22
From CSWiki
[edit] HW 4
[edit] Logic Puzzle
Appears to be working. Existing clues are not enough. You must also create code for the inferred clues as well.
Arbor Day Planting
Each of four men who live in Willowbrook Estates on the city’s north side, planted a different type of tree in their front yard on Arbor Day. From The clues, determine the first and last name of each man and the type of tree each planted.
1. George, Mr.Clary, and Mr. Becker (who got his tree from HomeTown Garden Center) all live within three blocks of one another.
2. Mr. Clary planted neither the cedar tree or the maple tree.
3. Harvey and Mr. Clary live next door to one another.
4. The ash tree was not planted by neither John nor Ivan.
5. The maple tree was not the tree planted by George (who is not surnamed Delgado).
6. Mr. Erichsen isn’t the man who planted the ash tree.
7. John, who purchased his tree at the Green Thumb Greenhouse, isn’t the man who planted the sycamore.
local
Trees = [ash cedar maple sycamore]
Names = [george harvey ivan john]
Surnames = [becker clary delgado erichsen]
% If Zs is not instantiated (or at least not bounded), then
% Xs and Ys should be. Otherwise 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
% IsA... procs.
proc {IsASurname ?Sur} {IsAMember Sur Surnames} end
proc {IsAName ?N} {IsAMember N Names} end
proc {IsATree ?T} {IsAMember T Trees} end
% Value is the value of field Property in the record SPs
% Both Property and Value may be uninstantiated
proc {IsAPropertyValue ABs ?Property ?Value}
{IsAMember Property#Value {Record.toListInd ABs}}
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 ABs}
N1 N2 in
{IsAPropertyValue ABs clary N1}
{IsAName N1}
{IsAPropertyValue ABs becker N2}
{IsAName N2}
N1 == N2 = false
ABs.becker == george = false
ABs.clary == george = false
end
proc {Clue2 ABs}
N2 N3 in
{IsAPropertyValue ABs cedar N2}
{IsAName N2}
{IsAPropertyValue ABs maple N3}
{IsAName N3}
N2 == N3 = false
ABs.cedar == ABs.clary = false
ABs.maple == ABs.clary = false
end
proc {Clue3 ABs}
ABs.clary == harvey = false
end
proc {Clue4 ABs}
N in
{IsAPropertyValue ABs ash N}
{IsAName N}
% eliminate all names that aren't john or ivan
N == john = false
N == ivan = false
% which means that if a name is chosen for ash it can't be chosen for:
ABs.cedar == N = false
ABs.maple == N = false
thread ABs.sycamore == N = false end
end
proc {Clue5 ABs}
N2 in
ABs.maple == george = false
{IsAPropertyValue ABs delgado N2}
{IsAName N2}
ABs.delgado == george = false
ABs.becker == N2 = false
ABs.clary == N2 = false
thread ABs.erichsen == N2 = false end
end
proc {Clue6 ABs}
N2 in
{IsAName N2}
ABs.becker == N2 = false
ABs.clary == N2 = false
ABs. delgado == N2 = false
{IsAPropertyValue ABs erichsen N2}
ABs.ash == ABs.erichsen = false
end
proc {Clue7 ABs}
ABs.becker == john = false
N1 N2 in
{IsAPropertyValue ABs sycamore N1}
{IsAName N1}
ABs.sycamore == john = false
ABs.ash == N1 = false
ABs.cedar == N1 = false
ABs.maple == N1 = false
end
fun {Arborists}
Properties = {FoldR [Surnames Trees] Append nil}
ABs = {MakeRecord arboristProperties Properties}
N1 N2
in
{Clue1 ABs}
{Clue2 ABs}
{Clue3 ABs}
{Clue4 ABs}
{Clue5 ABs}
{Clue6 ABs}
{Clue7 ABs}
ABs
end
in
{Browse {SearchAll Arborists}}
end

