Courses/CS 460/Fall 2005/Homework/Andre Liv/Oct 22
From CSWiki
[edit] Homework 4
=='''Homework 4'''==
<pre>
%This summer, five friends each found a different job to earn money. As it happened,
%they all started their new jobs in the same week; each on a different day and each
%earning a different amount. Determine the type of job each got, the day of the week
%each started working, each friend’s mode of transportation to their new job, and how
%much per hour each friend was earning.
%1. Chris didn’t have a newspaper route but he did use the bus. The boy who mowed lawns took the train to work.
%2. The boy who used a bicycle started working on Friday. Joe didn’t get a job as a painter
%3. The five friends are represented, in no particular order,
% by the following: the boy who made $6.00 per hour, the boy who started work on Thursday,
% the boy who used a car to get to work, Chris, and the boy who mowed lawns.
%4. Peter started work the day before the boy who worked as a cashier but two days after
% the boy who made $7.00 per hour. The boy who worked as a cashier made $0.50 per hour
% more than Greg made.
%5. The boy who started work on Tuesday made $6.25 per hour. Rob didn’t start work
% on Monday. Peter made more per hour than the painter but less than the boy who used a car.
%6. Rob walked to work. The person who started on Wednesday didn’t make $6.50 per hour.
% The golf caddy used a car to get to work.
local
HasName = ['chris' 'joe' 'peter' 'greg' 'rob']
HasDay = [friday thursday tuesday monday wednesday]
HasTransportation = [bus train bicycle car Walk]
HasJob = [painter cashier mowinglawn newspaperroute golfcaddy]
HasPay = ['6' '7' '0.5' '6.25' '6.50']
Groups = [HasName HasJob HasTransportation HasPay HasDay]
Properties = {FoldR Groups Append nil}
% 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 TPs
% Both Property and Value may be uninstantiated
proc {IsAPropertyValue TPs ?Property ?Value}
{IsAMember Property#Value {Record.toListInd TPs}}
end
% Name is a name
proc {IsAName ?Name} {IsAMember Name HasName} end
% Day is a day
proc {IsADay ?Day} {IsAMember Day HasDay} end
% Transportation is a transportation
proc {IsATransportation ?Transportation} {IsAMember Transportation HasTransportation} end
% Job is a job
proc {IsAJob ?Job} {IsAMember Job HasJob} end
% Pay rate is a pay rate
proc {IsAPay ?Pay} {IsAMember Pay HasPay} end
% Look at this neat definition for IsAMember/2.
% X is a member of Xs.
proc {IsAMember ?X Xs} {Append _ X|_ Xs} end
% Month1 precedes Month2 in Months.
% Instantiates the months if initially
% uninstantiated.
proc {EarlierInDay ?Day1 ?Day2}
Rest in
{IsATail Days Day1|Rest}
{IsAMember Day2 Rest}
end
%Chris didn’t have a newspaper route but he did use the bus.
%The boy who mowed lawns took the train to work.
proc {Clue1 SJs}
Job1 Name1 in
%Chris didn’t have a newspaper route but he did use the bus.
{IsAPropertyValue SJs Job1 'chris'}
%not a newspaper route
{IsAJob Job1}
Job1 == newspaperroute = false
%Chris takes the bus
{IsAPropertyValue SJs bus 'chris'}
%The boy who mowed lawns took the train to work
{IsAPropertyValue SJs mowinglawn Name1}
{IsAName Name1}
Name1 == 'chris' = false
{IsAPropertyValue SJs train Name1}
{IsAName Name1}
Name1 == 'chris' = false
end
%The boy who used a bicycle started working on Friday. Joe didn’t get a job as a painter
proc {Clue2 SJs}
Name1 Job1 in
% The boy who used a bicycle started working on Friday
{IsAPropertyValue SJs bicycle Name1}
{IsAName Name1}
Name1 == 'chris' = false
{IsAPropertyValue SJs friday Name1}
{IsAName Name1}
Name1 == 'chris' = false
% Joe didn’t get a job as a painter
{IsAPropertyValue SJs Job1 'joe'}
{IsAJob Job1}
Job1 == painter = false
end
% The five friends are represented, in no particular order,
% by the following: the boy who made $6.00 per hour, the boy who started work on Thursday,
% the boy who used a car to get to work, Chris, and the boy who mowed lawns.
proc {Clue3 SJs}
Name1 Job1 in
% the boy who made $6.00 per hour
%(by implication, Chris does not make $6.00/hr)
{IsAPropertyValue SJs '6' Name1}
{IsAName Name1}
Name1 == 'chris' = false
%the boy who started work on Thursday
{IsAPropertyValue SJs thursday Name1}
{IsAName Name1}
Name1 == 'chris' = false
%the boy who used a car to get to work
{IsAPropertyValue SJs car Name1}
{IsAName Name1}
Name1 == 'chris' = false
%the boy who mowed lawns
{IsAPropertyValue SJs mowinglawn Name1}
{IsAName Name1}
Name1 == 'chris' = false
end
% Peter started work the day before the boy who worked as a cashier but two days after
% the boy who made $7.00 per hour. The boy who worked as a cashier made $0.50 per hour
% more than Greg made
proc {Clue4 SJs}
Day1 Day2 Job1 Name1 Name2 Name3 in
% Peter started work the day before the boy who worked as
% a cashier but two days after the boy who made $7.00 per hour
{IsAPropertyValue SJs Day1 peter}
{IsAPropertyValue SJs Day2 Name1}
{EarlierInYear Day1 Day2}
{IsAName Name1}
Name1 == 'peter' = false
%but two days after the boy who made $7.00 per hour
{IsAPropertyValue SJs Day1 peter}
{IsAPropertyValue SJs Day2 Name2}
{IsAPropertyValue SJs '7' Name2}
{EarlierInYear Day2 Day1}
{IsAName Name1}
Name1 == 'peter' = false
%The boy who worked as a cashier made $0.50 per hour
% more than Greg made
{IsAPropertyValue SJs cashier Name3}
{IsAPropertyValue SJs Pay1 Name3}
{IsAPropertyValue SJs Pay2 'greg'}
{IsAPay Pay1}
{IsAPay Pay2}
(Pay1 == Pay2 + '0.5') = true
end
% The boy who started work on Tuesday made $6.25 per hour. Rob didn’t start work
% on Monday. Peter made more per hour than the painter but less than the boy who used a car.
proc {Clue5 SJs}
Name1 Name2 Name3 Pay1 Pay2 Job1 Day1 Pay1 in
% The boy who started work on Tuesday made $6.25 per hour
{IsAPropertyValue SJs '6.25' Name1}
{IsAName Name1}
Name1 == 'chris' = false
{IsAPropertyValue SJs tuesday Name1}
{IsAName Name1}
Name1 == 'chris' = false
%Rob didn’t start work on Monday
{IsAPropertyValue SJs Day1 'rob'}
{IsADay Day1}
Day1 == monday = false
%Peter made more per hour than the painter
{IsAPropertyValue SJs Pay1 'peter'}
{IsAPay Pay1}
{IsAPropertyValue SJs Pay2 Name2}
{IsAPay Pay2}
(Pay1 > Pay2) = true
{IsAName Name2}
Name2 == 'peter' = false
%but less than the boy who used a car
{IsAPropertyValue SJs Pay1 'peter'}
{IsAPay Pay1}
{IsAPropertyValue SJs Pay2 Name3}
{IsAPropertyValue SJs car Name3}
{IsAPay Pay2}
(Pay1 < Pay2) = true
{IsAName Name3}
Name3 == 'peter' = false
end
% Rob walked to work. The person who started on Wednesday didn’t make $6.50 per hour.
% The golf caddy used a car to get to work.
proc {Clue6 SJs}
Pay1 Name1 Name2 in
% Rob walked to work
{IsAPropertyValue SJs walk 'Rob'}
%The person who started on Wednesday didn’t make $6.50 per hour.
{IsAPropertyValue SJs wednesday Name1}
{IsAName Name1}
Name1 == rob = false
{IsAPropertyValue SJs Pay1 Name1}
{IsAName Name1}
Name1 == rob = false
{IsAPay Pay1}
Pay1 == '6.5' = false
%The golf caddy used a car to get to work.
{IsAPropertyValue SJs car Name2}
{IsAName Name2}
Name2 == rob = false
{IsAPropertyValue SJs golfcaddy Name2}
{IsAName Name2}
Name2 == rob = false
end
fun {SummerJob}
Properties = {FoldR [HasDay HasTransportation HasPay HasJob] Append nil}
SJs = {MakeRecord SJs Properties}
in
% To debug, comment out all the clues except the one
% you are working on and the ones that already work.
{Clue1 SJs}
{Clue2 SJs}
{Clue3 SJs}
{Clue4 SJs}
{Clue5 SJs}
{Clue6 SJs}
SJs
end
in
{Browse {SearchAll SummerJob}}
end
%I try to compile it but some errors?!!
</pre>
%I try to compile it but some errors?!!

