Courses/CS 460/Fall 2005/Homework/Jay Donnell/Oct 29

From CSWiki

Jump to: navigation, search

Most people think Mermaids are fictional characters or figments of a tired seaman’s imagination. However, recently a chest found (news reports on where it was found are not complete as yet) which belonged to the notorious pirate Red Beard. Red Beard had travelled the oceans and seas for years, and in a diary in the chest was a record of six mermaid sightings. According to the diary, Red Beard met each mermaid (one is Ariel) in a different body of water (one was the North Sea) during a different year (1690, 1695, 1700, 1705, 1710, or 1715). From the clues determine when and where Red Beard encountered each mermaid.

1. Fatima was not the mermaid Red Beard met in the Atlantic Ocean, nor in the sea in which he was sailing in 1715.

2. Red Beard met the lovely Ophelia in the Indian Ocean

3. In 1695 Red Beard was sailing in the Tasmania Sea and in 1710 in the Bering Sea.

4. Red Beard encountered Jasmine exactly five years before he encountered Ophelia.

5. In 1700 Red Beard was sailing in the Indian Ocean and in 1705 in the Pacific Ocean.

6. Red Beard encountered Druscilla exactly five years before he encountered Cassandra (which wasn’t 1710).

local
  Years = [1690 1695 1700 1705 1710 1715]
  Names = [fatima ophelia ariel jasmine druscilla cassandra]
  Seas = [indian north tasmania atlantic bering pacific]

  % 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

  % Value is the value of field Property in the record SPs
  % Both Property and Value may be uninstantiated
  proc {IsAPropertyValue SPs ?Property ?Value}
     {IsAMember Property#Value {Record.toListInd SPs}}
  end

  proc {IsAYear ?Year} {IsAMember Year Years} end

  proc {IsAName ?Name} {IsAMember Name Names} end

  proc {IsASea ?Sea} {IsAMember Sea Seas} 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 SPs}
     Year Year2
  in
     % 1. Fatima was not the mermaid Red Beard met in the Atlantic Ocean, nor in the sea in which he was sailing in 1715.
     {IsAYear Year}
     {IsAYear Year2}
     {IsAPropertyValue SPs fatima Year}
     {IsAPropertyValue SPs atlantic Year2}
     Year == 1715 = false
     Year2 == Year = false
     Year2 == 1715 = false
  end

  proc {Clue2 SPs}
     Year
  in
     % 2. Red Beard met the lovely Ophelia in the Indian Ocean
     {IsAYear Year}
     {IsAPropertyValue SPs indian Year}
     {IsAPropertyValue SPs ophelia Year}
     SPs.atlantic == Year = false
     SPs.fatima == Year = false
  end

  proc {Clue3 SPs}
     % 3. In 1695 Red Beard was sailing in the Tasmania Sea and in 1710 in the Bering Sea.
     
     {IsAPropertyValue SPs tasmania 1695}
     {IsAPropertyValue SPs bering 1710}
     
     SPs.atlantic == 1695 = false
     SPs.atlantic == 1710 = false
     SPs.indian == 1695 = false
     SPs.indian == 1710 = false
  end

  proc {Clue4 SPs}
     Tmp
  in
     % 4. Red Beard encountered Jasmine exactly five years before he encountered Ophelia.
     %{Browse 'OPHELIA'}
     %{Browse SPs.ophelia}
     Tmp = SPs.ophelia - 5
     {IsAPropertyValue SPs jasmine Tmp}
     SPs.fatima == Tmp = false
  end

  proc {Clue5 SPs}
     % 5. In 1700 Red Beard was sailing in the Indian Ocean and in 1705 in the Pacific Ocean.
     SPs.indian = 1700
     {IsAPropertyValue SPs pacific 1705}
     
     SPs.atlantic == SPs.pacific = false
     SPs.tasmania == SPs.pacific = false
     SPs.bering == SPs.pacific = false
  end

  proc {Clue6 SPs}
     Year Tmp Year2 Year3
  in
     % 6. Red Beard encountered Druscilla exactly five years before he encountered Cassandra (which wasn’t 1710).

     {IsAYear Year}
     {IsAPropertyValue SPs cassandra Year}
     Year == 1710 = false
     Year == 1690 = false
     SPs.fatima == Year = false
     SPs.jasmine == Year = false
     SPs.ophelia == Year = false
     
     %{Browse 'Cassandra'}
     Tmp = SPs.cassandra - 5
     {IsAPropertyValue SPs druscilla Tmp}
     SPs.fatima == Tmp = false
     SPs.jasmine == Tmp = false
     SPs.ophelia == Tmp = false

     % give ariel and north a value
     {IsAYear Year2}
     {IsAYear Year3}
     {IsAPropertyValue SPs north Year3}
     {IsAPropertyValue SPs ariel Year2}
     SPs.fatima == Year2 = false
     SPs.jasmine == Year2 = false
     SPs.ophelia == Year2 = false
     SPs.druscilla == Year2 = false
     SPs.cassandra == Year2 = false

     SPs.atlantic == Year3 = false
     SPs.pacific == Year3 = false
     SPs.indian == Year3 = false
     SPs.bering == Year3 = false
     SPs.tasmania == Year3 = false
  end

  fun {Mermaids}
     Properties = {FoldR [Names Seas] Append nil}
     SPs = {MakeRecord treeProperties Properties}
  in
     %{Browse SPs}
     % To debug, comment out all the clues except the one
     % you are working on and the ones that already work.
     {Clue1 SPs}
     {Clue2 SPs}
     {Clue3 SPs}
     {Clue4 SPs}
     {Clue5 SPs}
     {Clue6 SPs}

     %{Browse 'FINISHED'}
     SPs
  end
in
  {Browse {SearchAll Mermaids}}
end



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.

Before we start, our ABC grid looks like this:

local
  LastNames = [becker clary delgado erichsen]
  FirstNames = [george harvey ivan john]
  Trees = [ash cedar maple sycamore]

  % 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

  % Value is the value of field Property in the record SPs
  % Both Property and Value may be uninstantiated
  proc {IsAPropertyValue SPs ?Property ?Value}
     %{Browse 'IsAProp'}
     %{Browse SPs}
     %{Browse Property}
     %{Browse Value}
     {IsAMember Property#Value {Record.toListInd SPs}}
  end

  proc {IsALastName ?Name} {IsAMember Name LastNames} end

  proc {IsAFirstName ?Name} {IsAMember Name FirstNames} end

  proc {IsATree ?Tree} {IsAMember Tree Trees} 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 SPs}
     Tree1 Tree2 Tree3
  in
     {IsATree Tree1}
     {IsATree Tree2}
     {IsATree Tree3}
     Tree1 == Tree2 = false

     {IsAPropertyValue SPs george Tree3}
     {IsAPropertyValue SPs becker Tree1}
     {IsAPropertyValue SPs clary Tree2}

     SPs.george == Tree1 = false
     SPs.george == Tree2 = false
     %{Browse 'George'}
     %{Browse SPs.george}
  end

  proc {Clue2 SPs}
     Name Name2 in

     % Clary didn't plant a cedar or maple tree
     %{IsAPropertyValue SPs Name cedar}
     %{IsALastName Name}
     %Name == clary = false
     SPs.clary == cedar = false

     %{IsAPropertyValue SPs Name2 maple}
     %{IsALastName Name2}
     %Name2 == clary = false
     SPs.clary == maple = false
  end

  proc {Clue3 SPs}
     Tree1
  in
     {IsATree Tree1}
     {IsAPropertyValue SPs harvey Tree1}
     SPs.clary == Tree1 = false
     SPs.george == Tree1 = false
  end

  proc {Clue4 SPs}
     Tree Tree2
  in
     {IsAPropertyValue SPs john Tree}
     {IsAPropertyValue SPs ivan Tree2}
     {IsATree Tree}
     {IsATree Tree2}
     Tree == Tree2 = false
     Tree == ash = false
     Tree2 == ash = false

     %make sure it's not a duplicate
     SPs.harvey == Tree = false
     SPs.george == Tree = false
     SPs.harvey == Tree2 = false
     SPs.george == Tree2 = false
  end

  proc {Clue5 SPs}
     Tree
  in
     %{IsAPropertyValue SPs Name maple}
     %{IsALastName Name}
     {IsATree Tree}
     {IsAPropertyValue SPs delgado Tree}
     %{Browse 'delgado'}
     %{Browse Tree}
     SPs.george == Tree = false
     SPs.george == maple = false
     %Name == george = false

     %no duplicates
     SPs.clary == Tree = false
     SPs.becker == Tree = false
  end

  proc {Clue6 SPs}
     Tree
  in
     %{Browse 'SPs'}
     %{Browse SPs.erichsen}
     {IsATree Tree}
     {IsAPropertyValue SPs erichsen Tree}
     Tree == ash = false

     %no duplicates
     SPs.clary == Tree = false
     SPs.becker == Tree = false
     SPs.delgado == Tree = false
  end

  proc {Clue7 SPs}
     Name Tree
  in
     %{IsAPropertyValue SPs john Tree}
     %{IsAFirstName Name}
     %{IsATree Tree}
     SPs.john == sycamore = false
     SPs.becker == SPs.john = false

  end

  fun {FiveSisters}
     Properties = {FoldR [FirstNames LastNames] Append nil}
     SPs = {MakeRecord treeProperties Properties}
  in
     %{Browse SPs}
     % To debug, comment out all the clues except the one
     % you are working on and the ones that already work.
     {Clue1 SPs}
     {Clue2 SPs}
     {Clue3 SPs}
     {Clue4 SPs}
     {Clue5 SPs}
     {Clue6 SPs}
     {Clue7 SPs}

     %{Browse 'FINISHED'}
     SPs
  end
in
  {Browse {SearchAll FiveSisters}}
end



On Sunday, the Millers would be celebrating their 50th wedding anniversary and their four daughters were coming home for the weekend. As one of their gifts, the girls were taking their Mom to their favorite salon for a complete make-over, and while at it each daughter - Dolly, Holly, Molly, and Polly - each wanted to visit the salon for a special service. One by one the girls called and asked their Mom to make the appointments ahead of time so they could all get in together, and she complied with enthusiasm. Somehow Mrs. Miller got confused on which girl wanted which service. When they all arrived at the salon, it didn’t take long to get things cleared up, however. From the clues, determine the service each daughter wanted done and which service the salon had them in the appointment book for.

1. Three of the four sisters are Molly, the daughter who wanted her hair highlighted, and the daughter who was down in the appointment book for a permanent.

2. Dolly and Polly neither one wanted either a cut and style or manicure.

3. The daughter who wanted her hair cut and styled was down in the salon’s appointment book for a manicure.

4. Dolly wasn’t down in the salon’s appointment book for either a permanent nor a cut and style.

This one does not give a unique answer and I can't figure out the unique answer on my own.

local
  Appts = [aHighlight aCut aManicure aPerm]
  Names = [molly holly dolly polly]
  Wants = [wHighlight wCut wManicure wPerm]

  % 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

  % Value is the value of field Property in the record SPs
  % Both Property and Value may be uninstantiated
  proc {IsAPropertyValue SPs ?Property ?Value}
     {IsAMember Property#Value {Record.toListInd SPs}}
  end

  proc {IsAAppt ?Appt} {IsAMember Appt Appts} end

  proc {IsAName ?Name} {IsAMember Name Names} end

  proc {IsAWant ?Want} {IsAMember Want Wants} 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 SPs}
     Name Name2 Name3 Name4
  in
     %1. Three of the four sisters are Molly, the daughter who wanted her hair highlighted, and the daughter who was down in the appointment book for a permanent.

     {IsAName Name}
     {IsAName Name2}
     {IsAName Name3}
     {IsAName Name4}
     
     {IsAPropertyValue SPs wHighlight Name}
     {IsAPropertyValue SPs aHighlight Name3}
     Name3 == Name = false
     
     {IsAPropertyValue SPs aPerm Name2}
     {IsAPropertyValue SPs wPerm Name4}
     Name4 == Name2 = false
     Name4 == Name = false
     Name2 == Name3 = false

     Name == molly = false
     Name2 == molly = false
     
  end

  proc {Clue2 SPs}
     Name Name2 Name3 Name4
  in
     %2. Dolly and Polly neither one wanted either a cut and style or manicure.

     {IsAName Name}
     {IsAName Name2}
     {IsAName Name3}
     {IsAName Name4}
     
     {IsAPropertyValue SPs wCut Name}
     {IsAPropertyValue SPs aCut Name3}     
     Name == Name3 = false
     SPs.aPerm == Name3 = false
     SPs.aHighlight == Name3 = false
     
     {IsAPropertyValue SPs wManicure Name2}
     {IsAPropertyValue SPs aManicure Name4}
     Name2 == Name4 = false
     SPs.aPerm == Name4 = false
     SPs.aHighlight == Name4 = false
     
     Name == dolly = false
     Name == polly = false
     Name2 == dolly = false
     Name2 == polly = false

     SPs.wHighlight == Name = false
     SPs.wHighlight == Name2 = false
     SPs.wPerm == Name = false
     SPs.wPerm == Name2 = false
  end

  proc {Clue3 SPs}
     Name
  in
     % 3. The daughter who wanted her hair cut and styled was down in the salon’s appointment book for a manicure.

     SPs.wCut == SPs.aManicure = true
  end
  

  proc {Clue4 SPs}
     Name
  in
     % 4. Dolly wasn’t down in the salon’s appointment book for either a permanent nor a cut and style.

     SPs.aPerm == dolly = false
     SPs.aCut == dolly = false
     
  end


  fun {Appointments}
     Properties = {FoldR [Appts Wants] Append nil}
     SPs = {MakeRecord treeProperties Properties}
  in
     %{Browse SPs}
     % To debug, comment out all the clues except the one
     % you are working on and the ones that already work.
     {Clue1 SPs}
     {Clue2 SPs}
     {Clue3 SPs}
     {Clue4 SPs}

     %{Browse 'FINISHED'}
     SPs
  end
in
  {Browse {SearchAll Appointments}}
end