TBD.my

Full Version: [WTA] masalah prolog
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Code:
hotel:-
    printprompt,
    readsentence(S),
    reply(S).

/*  Computer's response  */
%exits program
reply(['bye']):- write('BYE'), nl.
%finds valid question, prints response.
reply(S):-
    sentence(S,[]),
    hotel.
%if no valid question asks to re-phrase question.
reply(S):-
    write('Please re-phrase your question'),nl,nl,
    hotel.

printprompt:- write('hotel>>').


/*  ----  Grammar  ----  */

sentence --> booking, room(RT1,RT2,NI,Nu,_),
    {bookroom(RT1,RT2,NI,Nu)}.

sentence --> availability, room(RT1,RT2,_,Nu,_), free(_), night(NI),
    {availability(RT1,RT2,NI,Nu)}.

sentence --> price1, room(RT1,RT2,NI,Nu,_),
    {roomprice(RT1,RT2,NI,Nu)}.

sentence --> price2, room(RT1,RT2,NI,Nu,_), cost,
    {roomprice(RT1,RT2,NI,Nu)}.

sentence --> quword, room1(RT1,RT2,_,plur), are, free(2), night(NI),
    {listrooms(RT1,RT2,NI)}.

sentence --> confirm, reservation(NI),
    {confirmbooking(NI)}.

sentence --> ['Has'], name(Na,Ti), bookedin(_), night(_),
    {bookedinyet(Ti,[Na])}.
sentence --> ['Has'], title(Ti), [Na], bookedin(_), night(_),
    {unknownname(Ti,Na)}.

sentence --> name(Na,Ti), [has], bookedin(1),
    {bookedin(Ti,[Na])}.
sentence --> title(Ti), [Na], [has], bookedin(1),
    {unknownname(Ti,Na)}.

sentence --> name(Na,Ti), [has], left,
    {bookedout(Ti,[Na])}.
sentence --> title(Ti), [Na], [has], left,
    {unknownname(Ti,Na)}.

are --> [are].
are --> [do,you,have].

left --> [left].
left --> [booked,out].
left --> [left,the,hotel].

booking --> polite1, [book,me].
booking --> ['Can','I',have].
booking --> ['Can','I',book].
booking --> ['Please',can,'I',have].
booking --> ['I',want,to,reserve].

availibility --> ['Do',you,have].
availibility --> ['Is',there].

price1 --> polite, ['What',is,the,price,of].
price1 --> polite, ['How',much,is].
price1 --> polite, ['How',much,would,it,cost,for].
price2 --> polite, ['How',much,does].
price2 --> polite, ['How',much,would].

confirm --> polite1, [confirm].
confirm --> polite1, [check].
confirm --> [confirm].
confirm --> [check].

reservation(NI) --> [my,reservation], night(NI).
reservation(NI) --> [my,booking], night(NI).
reservation(NI) --> [my,reservations], night(NI).
reservation(NI) --> [my,bookings], night(NI).
reservation(NI) --> [my], room(NI,_).

bookedin(1) --> [booked,into,the,hotel].
bookedin(1) --> [booked,in].
bookedin(1) --> [arrived].
bookedin(2) --> [booked,into,the,hotel,yet].
bookedin(2) --> [booked,in,yet].

room(RT1,RT2,NI,Nu,SP) --> number(SP,Nu), room1(RT1,RT2,NI,SP).

room1(0,0,NI,SP) --> room2(SP), night(NI).
room1(RT1,0,NI,SP) --> roomtype1(RT1), room2(SP), night(NI).

%allow multiple rooms with shower - common mistake don't want to annoy customer

room1(RT1,RT2,NI,SP) --> roomtype1(RT1), room2(SP), night(NI), roomtype2(RT2,_).
room1(RT1,RT2,NI,SP) --> roomtype1(RT1), room2(SP), roomtype2(RT2,_), night(NI).

roomtype1(single) --> [single].
roomtype1(double) --> [double].
roomtype1(0) --> [].

roomtype2(yes,sing) --> [with,shower].
roomtype2(yes,sing) --> [with,a,shower].
roomtype2(yes,plur) --> [with,showers].
roomtype2(yes,_) --> [yes].
roomtype2(no,sing) --> [without,shower].
roomtype2(no,sing) --> [without,a,shower].
roomtype2(no,plur) --> [without,showers].
roomtype2(no,_) --> [no].
roomtype2(0,_) --> [].

room2(sing) --> [room].
room2(plur) --> [rooms].

number(sing,1) --> [a].
number(sing,1) --> [one].
number(plur,2) --> [two].
number(plur,3) --> [three].

night(0) --> [].
night(NI) --> [for], night1(NI).
night(NI) --> night1(NI).

night1(1) --> [tonight].
night1(1) --> [today].
night1(1) --> [this,evening].
night1(2) --> [tomorrow].
night1(2) --> [tomorrow,evening].
night1(2) --> [tomorrow,night].

cost --> [cost].
cost --> [cost,me].

polite --> polite1, polite2.
polite --> [].

polite1 --> ['Please'].
polite1 --> ['Would',you].
polite1 --> ['Could',you].
polite1 --> ['Please',would,you].
polite1 --> ['Please',could,you].
polite1 --> [].

polite2 --> [let,me,know].
polite2 --> [tell,me].

%number indicates if can be blank or not.
free(1) --> [].
free(2) --> [free].
free(2) --> [available].
free(2) --> [vacant].

quword --> ['What'].
quword --> ['Which'].

title(1) --> ['Mr'].
title(2) --> ['Mrs'].
title(3) --> ['Miss'].
title(4) --> ['Ms'].

name(Na,Ti,[Title|[Na|T]],T):-
    title(Ti,[Title],_),
    customer(Ti,[Na],_).



/*  ----  Data Base  ----  */

%roomdb(roomnumber,roomtype,shower,price).
roomdb(1,double,no,35).
roomdb(2,double,yes,40).
roomdb(3,single,yes,25).
roomdb(4,single,no,20).
roomdb(5,single,yes,25).

:-dynamic roombooking/3.
%roombooking(roomnumber,day,name)
roombooking(1,1,roomfree).
roombooking(1,2,roomfree).
roombooking(2,1,roomfree).
roombooking(2,2,roomfree).
roombooking(3,1,roomfree).
roombooking(3,2,roomfree).
roombooking(4,1,roomfree).
roombooking(4,2,roomfree).
roombooking(5,1,roomfree).
roombooking(5,2,roomfree).

:-dynamic customer/3.
%customer(title,name,bookedin).


/*  ----  Data Base Funtions  ----  */


%prints list of rooms available
printlist(Before,[H|T],After):-
    write(Before),
    write(H),
    write(After), nl,
    printlist(Before,T,After).
printlist(_,[],_).

%lists rooms available
listrooms(RT1,RT2,NI1):-
    %if roomtype 0 changes it to _
    chkroomtype1(1,RT1,Re1),
    chkroomtype2(1,RT2,Re2),
    %if night 0 askswhich night
    checknight(NI1,NI2),
    %gets all results in a list
    findall(RMNUMBER,roomfree(RMNUMBER,Re1,Re2,NI2), Rooms),
    %gets length of the list
    list_length(Rooms,L),
    listrooms2(Rooms,L).

%if no rooms
listrooms2(roomfree,0):-
    write('no rooms are free.'),nl,nl.
%if rooms
listrooms2(Rooms,_):-
    printlist(('Room '),Rooms,(' is free.')),nl.

%books a room
bookroom(RT1,RT2,NI1,Nu):-
    %if type or room not specified asks them to be entered.
    chkroomtype1(2,RT1,Re1),
    chkroomtype2(2,RT2,Re2),
    checknight(NI1,NI2),
    !,
    bookroom2(Re1,Re2,NI2,Nu).

%if room is available asks for customer name
bookroom2(Re1,Re2,NI,Nu,RMNUMBER):-
    roomfree(RMNUMBER,Re1,Re2,NI),
    getname(Ti,Na),
    assert(customer(Ti,Na,no)),
    bookroom3(Re1,Re2,NI,Na,Nu).

%if no rooms available tells customer
bookroom2(Re1,Re2,NI):-
    write('sorry, we were unable to complete your request.'), nl,
    write('We did not have sufficient rooms available'), nl,nl.

%when room list is empty
bookroom3(Re1,Re2,NI,Na2,0).
%books rooms in list one at a time
bookroom3(Re1,Re2,NI,Na2,Nu):-
    Nu \== 0,
    roomfree(RMNUMBER,Re1,Re2,NI),
    retract(roombooking(RMNUMBER,NI,_)),
    assert(roombooking(RMNUMBER,NI,Na2)),
    write('You have been booked into room '),
    night1(NI,[NIGHT,_],_),
    write(RMNUMBER),
    write(' for '),
    write(NIGHT),
    write('.'),nl,nl,
    Nu2 is Nu - 1,
    bookroom3(Re1,Re2,NI,Na2,Nu2).
%if above fails then can't book all rooms, tell customer.
bookroom3(Re1,Re2,NI,Na2,Nu):-
    write('sorry, we were unable to book all of your rooms.'), nl,
    write('We did not have sufficient rooms available'), nl,nl.

%gets room price.
roomprice(RT1,RT2):-
    %gets input if supplied as 0
    chkroomtype1(2,RT1,Re1),
    chkroomtype2(2,RT2,Re2),
    %gets price
    roomdb(_,Re1,Re2,Price),
    %can't get pound synbol for some reason.
    %displays price
    write('That room costs $'),
    write(Price),
    write('.'),nl,
    roomprice2(Re1,Re2).

%if one available tonight tells customer
roomprice2(RT1,RT2):-
    roomfree(_,RT1,RT2,1),
    write('We have one available tonight.'),nl,nl.
roomprice2(RT1,RT2):-
    nl.

%checks for room availability
availability(RT1,RT2,NI,Nu):-
    %validates input
    chkroomtype1(1,RT1,Re1),
    chkroomtype2(1,RT2,Re2),
    checknight(NI,NI2),
    availiability2(Re1,Re2,NI2,Nu).
%room free
availiability2(RT1,RT2,NI,Nu):-
    roomfree(_,RT1,RT2,NI),
    write('Yes, we have a room available.'),nl,nl.
%room not free
availiability2(RT1,RT2,NI,Nu):-
    write('No, Im sorry but all our rooms of that type are full.'),nl,nl.

%confirms customer booking
confirmbooking(NI):-
    checknight(NI,NI2),
    %gets name
    getname(Ti,Na),
    %gets all bookings
    findall(RoomNumber,roombooking(RoomNumber,NI2,Na),Bookings),
    list_length(Bookings,NumB),
    confirmbooking2(Bookings,NumB,NI2).
%no bookings
confirmbooking2(Bookings,0,NI):-
    write('You have no reservations.'),nl,nl.
%lists bookings
confirmbooking2(Bookings,NumB,NI):-
    NumB \== 0,
    night1(NI,[Night,_],_),
    write('Your reservations for '),
    write(Night),
    write(' are listed below:'),nl,
    printreslist(Bookings),nl.

%if unknown name entered
unknownname(Ti,Na):-
    write('I do not know '),
    title(Ti,[Title,_],_),
    write(Title),
    write(' '),
    write(Na),
    write('.'),nl,nl.

%has a customer booked in yet
bookedinyet(Ti,Na):-
    %consult db
    customer(Ti,Na,yes),
    %writes yes
    write('Yes '),
    title(Ti,[Title,_],_),
    write(Title),
    write(' '),
    namefromlist(Na,Name),
    write(Name),
    write(' has booked in.'),nl,nl,!.
%not booked in
bookedinyet(Ti,Na):-
    customer(Ti,Na,no),
    write('No '),
    title(Ti,[Title,_],_),
    write(Title),
    write(' '),
    namefromlist(Na,Name),
    write(Name),
    write(' has not booked in yet.'),nl,nl,!.

%marks customer as booked in
bookedin(Ti,Na):-
    retract(customer(Ti,Na,_)),
    assert(customer(Ti,Na,yes)),
    title(Ti,[Title,_],_),
    write(Title),
    write(' '),
    namefromlist(Na,Name),
    write(Name),
    write(' booked in.'),nl,nl.

%deletes name and all reservations
bookedout(Ti,Na):-
    retract(customer(Ti,Na,_)),
    findall([RmNumber,Ni],roombooking(RmNumber,Ni,Na),Bookings),
    bookedout2(Bookings),
    title(Ti,[Title,_],_),
    write(Title),
    write(' '),
    namefromlist(Na,Name),
    write(Name),
    write(' has been deleted. Rooms marked as free.'),nl,nl.

bookedout2([]).
bookedout2([[RmNumber|[Ni]]|T]):-
    retract(roombooking(RmNumber,Ni,_)),
    assert(roombooking(RmNumber,Ni,roomfree)),
    bookedout2(T).

namefromlist([Na],Na).

printreslist([]).
printreslist([H|T]):-
    roomdb(H,Type1,Type2,Price),
    write('Room '),
    write(H),
    write(', '),
    write(Type1),
    write(', with shower: '),
    write(Type2),
    write(', $'),
    write(Price),
    write('.'),nl,
    printreslist(T).

roomfree(RMNUMBER,Type1,Type2,Day):-
    roomdb(RMNUMBER,Type1,Type2,_),
    roombooking(RMNUMBER,Day,roomfree).

%gets users name
getname(Ti,Na2):-
    write('please enter your name(e.g. Mr Bloggs.)'), nl,
    readsentence(Na1),
    title(Ti,Na1,Na2),!.
%repeats until valid name entered
getname(Ti,Na):-
    getname(Ti,Na).

%roomtype_(getif0,typenumber,description)
chkroomtype1(_,single,single).
chkroomtype1(_,double,double).
chkroomtype1(1,0,_).
chkroomtype1(2,0,RmType):-
    write('please enter roomtype (single/double)'), nl,
    readsentence(RMTYPE),
    %check input against grammar (single/double entered)
    roomtype1(RmType,RMTYPE,_).
%repeat if not found the first time(invalid input)
chkroomtype1(2,0,RMTYPE):-
    chkroomtype1(2,0,RMTYPE).

chkroomtype2(_,yes,yes).
chkroomtype2(_,no,no).
chkroomtype2(1,0,_).
chkroomtype2(2,0,RmType):-
    write('room with a shower (yes,no)'), nl,
    readsentence(RMTYPE),
    %check grammar
    roomtype2(Number,_,RMTYPE,_).
chkroomtype2(2,0,RMTYPE):-
    chkroomtype2(2,0,RMTYPE).

nightinput(In,Ni,roomfree):-
    In == Ni,!.
nightinput(In,Ni,_):-
    In \== Ni,!.

checknight(0,NIo):-
    write('please enter night(tonight/tomorrow)'), nl,
    readsentence(In),
    night1(NIo,In,_).
checknight(0,NIo):-
    checknight(0,NIo).
checknight(NI,NI):-
    NI \==  0.

list_length([], 0).
list_length([_|T], N):-
    list_length(T, N1),
    N is N1 + 1.



xleh kompile
apa error dia kasi?

(27-12-2011, 06:26 PM)ikhsan Wrote: [ -> ]
Code:
hotel:-
    printprompt,
    readsentence(S),
    reply(S).

/*  Computer's response  */
%exits program
reply(['bye']):- write('BYE'), nl.
%finds valid question, prints response.
reply(S):-
    sentence(S,[]),
    hotel.
%if no valid question asks to re-phrase question.
reply(S):-
    write('Please re-phrase your question'),nl,nl,
    hotel.

printprompt:- write('hotel>>').


/*  ----  Grammar  ----  */

sentence --> booking, room(RT1,RT2,NI,Nu,_),
    {bookroom(RT1,RT2,NI,Nu)}.

sentence --> availability, room(RT1,RT2,_,Nu,_), free(_), night(NI),
    {availability(RT1,RT2,NI,Nu)}.

sentence --> price1, room(RT1,RT2,NI,Nu,_),
    {roomprice(RT1,RT2,NI,Nu)}.

sentence --> price2, room(RT1,RT2,NI,Nu,_), cost,
    {roomprice(RT1,RT2,NI,Nu)}.

sentence --> quword, room1(RT1,RT2,_,plur), are, free(2), night(NI),
    {listrooms(RT1,RT2,NI)}.

sentence --> confirm, reservation(NI),
    {confirmbooking(NI)}.

sentence --> ['Has'], name(Na,Ti), bookedin(_), night(_),
    {bookedinyet(Ti,[Na])}.
sentence --> ['Has'], title(Ti), [Na], bookedin(_), night(_),
    {unknownname(Ti,Na)}.

sentence --> name(Na,Ti), [has], bookedin(1),
    {bookedin(Ti,[Na])}.
sentence --> title(Ti), [Na], [has], bookedin(1),
    {unknownname(Ti,Na)}.

sentence --> name(Na,Ti), [has], left,
    {bookedout(Ti,[Na])}.
sentence --> title(Ti), [Na], [has], left,
    {unknownname(Ti,Na)}.

are --> [are].
are --> [do,you,have].

left --> [left].
left --> [booked,out].
left --> [left,the,hotel].

booking --> polite1, [book,me].
booking --> ['Can','I',have].
booking --> ['Can','I',book].
booking --> ['Please',can,'I',have].
booking --> ['I',want,to,reserve].

availibility --> ['Do',you,have].
availibility --> ['Is',there].

price1 --> polite, ['What',is,the,price,of].
price1 --> polite, ['How',much,is].
price1 --> polite, ['How',much,would,it,cost,for].
price2 --> polite, ['How',much,does].
price2 --> polite, ['How',much,would].

confirm --> polite1, [confirm].
confirm --> polite1, [check].
confirm --> [confirm].
confirm --> [check].

reservation(NI) --> [my,reservation], night(NI).
reservation(NI) --> [my,booking], night(NI).
reservation(NI) --> [my,reservations], night(NI).
reservation(NI) --> [my,bookings], night(NI).
reservation(NI) --> [my], room(NI,_).

bookedin(1) --> [booked,into,the,hotel].
bookedin(1) --> [booked,in].
bookedin(1) --> [arrived].
bookedin(2) --> [booked,into,the,hotel,yet].
bookedin(2) --> [booked,in,yet].

room(RT1,RT2,NI,Nu,SP) --> number(SP,Nu), room1(RT1,RT2,NI,SP).

room1(0,0,NI,SP) --> room2(SP), night(NI).
room1(RT1,0,NI,SP) --> roomtype1(RT1), room2(SP), night(NI).

%allow multiple rooms with shower - common mistake don't want to annoy customer

room1(RT1,RT2,NI,SP) --> roomtype1(RT1), room2(SP), night(NI), roomtype2(RT2,_).
room1(RT1,RT2,NI,SP) --> roomtype1(RT1), room2(SP), roomtype2(RT2,_), night(NI).

roomtype1(single) --> [single].
roomtype1(double) --> [double].
roomtype1(0) --> [].

roomtype2(yes,sing) --> [with,shower].
roomtype2(yes,sing) --> [with,a,shower].
roomtype2(yes,plur) --> [with,showers].
roomtype2(yes,_) --> [yes].
roomtype2(no,sing) --> [without,shower].
roomtype2(no,sing) --> [without,a,shower].
roomtype2(no,plur) --> [without,showers].
roomtype2(no,_) --> [no].
roomtype2(0,_) --> [].

room2(sing) --> [room].
room2(plur) --> [rooms].

number(sing,1) --> [a].
number(sing,1) --> [one].
number(plur,2) --> [two].
number(plur,3) --> [three].

night(0) --> [].
night(NI) --> [for], night1(NI).
night(NI) --> night1(NI).

night1(1) --> [tonight].
night1(1) --> [today].
night1(1) --> [this,evening].
night1(2) --> [tomorrow].
night1(2) --> [tomorrow,evening].
night1(2) --> [tomorrow,night].

cost --> [cost].
cost --> [cost,me].

polite --> polite1, polite2.
polite --> [].

polite1 --> ['Please'].
polite1 --> ['Would',you].
polite1 --> ['Could',you].
polite1 --> ['Please',would,you].
polite1 --> ['Please',could,you].
polite1 --> [].

polite2 --> [let,me,know].
polite2 --> [tell,me].

%number indicates if can be blank or not.
free(1) --> [].
free(2) --> [free].
free(2) --> [available].
free(2) --> [vacant].

quword --> ['What'].
quword --> ['Which'].

title(1) --> ['Mr'].
title(2) --> ['Mrs'].
title(3) --> ['Miss'].
title(4) --> ['Ms'].

name(Na,Ti,[Title|[Na|T]],T):-
    title(Ti,[Title],_),
    customer(Ti,[Na],_).



/*  ----  Data Base  ----  */

%roomdb(roomnumber,roomtype,shower,price).
roomdb(1,double,no,35).
roomdb(2,double,yes,40).
roomdb(3,single,yes,25).
roomdb(4,single,no,20).
roomdb(5,single,yes,25).

:-dynamic roombooking/3.
%roombooking(roomnumber,day,name)
roombooking(1,1,roomfree).
roombooking(1,2,roomfree).
roombooking(2,1,roomfree).
roombooking(2,2,roomfree).
roombooking(3,1,roomfree).
roombooking(3,2,roomfree).
roombooking(4,1,roomfree).
roombooking(4,2,roomfree).
roombooking(5,1,roomfree).
roombooking(5,2,roomfree).

:-dynamic customer/3.
%customer(title,name,bookedin).


/*  ----  Data Base Funtions  ----  */


%prints list of rooms available
printlist(Before,[H|T],After):-
    write(Before),
    write(H),
    write(After), nl,
    printlist(Before,T,After).
printlist(_,[],_).

%lists rooms available
listrooms(RT1,RT2,NI1):-
    %if roomtype 0 changes it to _
    chkroomtype1(1,RT1,Re1),
    chkroomtype2(1,RT2,Re2),
    %if night 0 askswhich night
    checknight(NI1,NI2),
    %gets all results in a list
    findall(RMNUMBER,roomfree(RMNUMBER,Re1,Re2,NI2), Rooms),
    %gets length of the list
    list_length(Rooms,L),
    listrooms2(Rooms,L).

%if no rooms
listrooms2(roomfree,0):-
    write('no rooms are free.'),nl,nl.
%if rooms
listrooms2(Rooms,_):-
    printlist(('Room '),Rooms,(' is free.')),nl.

%books a room
bookroom(RT1,RT2,NI1,Nu):-
    %if type or room not specified asks them to be entered.
    chkroomtype1(2,RT1,Re1),
    chkroomtype2(2,RT2,Re2),
    checknight(NI1,NI2),
    !,
    bookroom2(Re1,Re2,NI2,Nu).

%if room is available asks for customer name
bookroom2(Re1,Re2,NI,Nu,RMNUMBER):-
    roomfree(RMNUMBER,Re1,Re2,NI),
    getname(Ti,Na),
    assert(customer(Ti,Na,no)),
    bookroom3(Re1,Re2,NI,Na,Nu).

%if no rooms available tells customer
bookroom2(Re1,Re2,NI):-
    write('sorry, we were unable to complete your request.'), nl,
    write('We did not have sufficient rooms available'), nl,nl.

%when room list is empty
bookroom3(Re1,Re2,NI,Na2,0).
%books rooms in list one at a time
bookroom3(Re1,Re2,NI,Na2,Nu):-
    Nu \== 0,
    roomfree(RMNUMBER,Re1,Re2,NI),
    retract(roombooking(RMNUMBER,NI,_)),
    assert(roombooking(RMNUMBER,NI,Na2)),
    write('You have been booked into room '),
    night1(NI,[NIGHT,_],_),
    write(RMNUMBER),
    write(' for '),
    write(NIGHT),
    write('.'),nl,nl,
    Nu2 is Nu - 1,
    bookroom3(Re1,Re2,NI,Na2,Nu2).
%if above fails then can't book all rooms, tell customer.
bookroom3(Re1,Re2,NI,Na2,Nu):-
    write('sorry, we were unable to book all of your rooms.'), nl,
    write('We did not have sufficient rooms available'), nl,nl.

%gets room price.
roomprice(RT1,RT2):-
    %gets input if supplied as 0
    chkroomtype1(2,RT1,Re1),
    chkroomtype2(2,RT2,Re2),
    %gets price
    roomdb(_,Re1,Re2,Price),
    %can't get pound synbol for some reason.
    %displays price
    write('That room costs $'),
    write(Price),
    write('.'),nl,
    roomprice2(Re1,Re2).

%if one available tonight tells customer
roomprice2(RT1,RT2):-
    roomfree(_,RT1,RT2,1),
    write('We have one available tonight.'),nl,nl.
roomprice2(RT1,RT2):-
    nl.

%checks for room availability
availability(RT1,RT2,NI,Nu):-
    %validates input
    chkroomtype1(1,RT1,Re1),
    chkroomtype2(1,RT2,Re2),
    checknight(NI,NI2),
    availiability2(Re1,Re2,NI2,Nu).
%room free
availiability2(RT1,RT2,NI,Nu):-
    roomfree(_,RT1,RT2,NI),
    write('Yes, we have a room available.'),nl,nl.
%room not free
availiability2(RT1,RT2,NI,Nu):-
    write('No, Im sorry but all our rooms of that type are full.'),nl,nl.

%confirms customer booking
confirmbooking(NI):-
    checknight(NI,NI2),
    %gets name
    getname(Ti,Na),
    %gets all bookings
    findall(RoomNumber,roombooking(RoomNumber,NI2,Na),Bookings),
    list_length(Bookings,NumB),
    confirmbooking2(Bookings,NumB,NI2).
%no bookings
confirmbooking2(Bookings,0,NI):-
    write('You have no reservations.'),nl,nl.
%lists bookings
confirmbooking2(Bookings,NumB,NI):-
    NumB \== 0,
    night1(NI,[Night,_],_),
    write('Your reservations for '),
    write(Night),
    write(' are listed below:'),nl,
    printreslist(Bookings),nl.

%if unknown name entered
unknownname(Ti,Na):-
    write('I do not know '),
    title(Ti,[Title,_],_),
    write(Title),
    write(' '),
    write(Na),
    write('.'),nl,nl.

%has a customer booked in yet
bookedinyet(Ti,Na):-
    %consult db
    customer(Ti,Na,yes),
    %writes yes
    write('Yes '),
    title(Ti,[Title,_],_),
    write(Title),
    write(' '),
    namefromlist(Na,Name),
    write(Name),
    write(' has booked in.'),nl,nl,!.
%not booked in
bookedinyet(Ti,Na):-
    customer(Ti,Na,no),
    write('No '),
    title(Ti,[Title,_],_),
    write(Title),
    write(' '),
    namefromlist(Na,Name),
    write(Name),
    write(' has not booked in yet.'),nl,nl,!.

%marks customer as booked in
bookedin(Ti,Na):-
    retract(customer(Ti,Na,_)),
    assert(customer(Ti,Na,yes)),
    title(Ti,[Title,_],_),
    write(Title),
    write(' '),
    namefromlist(Na,Name),
    write(Name),
    write(' booked in.'),nl,nl.

%deletes name and all reservations
bookedout(Ti,Na):-
    retract(customer(Ti,Na,_)),
    findall([RmNumber,Ni],roombooking(RmNumber,Ni,Na),Bookings),
    bookedout2(Bookings),
    title(Ti,[Title,_],_),
    write(Title),
    write(' '),
    namefromlist(Na,Name),
    write(Name),
    write(' has been deleted. Rooms marked as free.'),nl,nl.

bookedout2([]).
bookedout2([[RmNumber|[Ni]]|T]):-
    retract(roombooking(RmNumber,Ni,_)),
    assert(roombooking(RmNumber,Ni,roomfree)),
    bookedout2(T).

namefromlist([Na],Na).

printreslist([]).
printreslist([H|T]):-
    roomdb(H,Type1,Type2,Price),
    write('Room '),
    write(H),
    write(', '),
    write(Type1),
    write(', with shower: '),
    write(Type2),
    write(', $'),
    write(Price),
    write('.'),nl,
    printreslist(T).

roomfree(RMNUMBER,Type1,Type2,Day):-
    roomdb(RMNUMBER,Type1,Type2,_),
    roombooking(RMNUMBER,Day,roomfree).

%gets users name
getname(Ti,Na2):-
    write('please enter your name(e.g. Mr Bloggs.)'), nl,
    readsentence(Na1),
    title(Ti,Na1,Na2),!.
%repeats until valid name entered
getname(Ti,Na):-
    getname(Ti,Na).

%roomtype_(getif0,typenumber,description)
chkroomtype1(_,single,single).
chkroomtype1(_,double,double).
chkroomtype1(1,0,_).
chkroomtype1(2,0,RmType):-
    write('please enter roomtype (single/double)'), nl,
    readsentence(RMTYPE),
    %check input against grammar (single/double entered)
    roomtype1(RmType,RMTYPE,_).
%repeat if not found the first time(invalid input)
chkroomtype1(2,0,RMTYPE):-
    chkroomtype1(2,0,RMTYPE).

chkroomtype2(_,yes,yes).
chkroomtype2(_,no,no).
chkroomtype2(1,0,_).
chkroomtype2(2,0,RmType):-
    write('room with a shower (yes,no)'), nl,
    readsentence(RMTYPE),
    %check grammar
    roomtype2(Number,_,RMTYPE,_).
chkroomtype2(2,0,RMTYPE):-
    chkroomtype2(2,0,RMTYPE).

nightinput(In,Ni,roomfree):-
    In == Ni,!.
nightinput(In,Ni,_):-
    In \== Ni,!.

checknight(0,NIo):-
    write('please enter night(tonight/tomorrow)'), nl,
    readsentence(In),
    night1(NIo,In,_).
checknight(0,NIo):-
    checknight(0,NIo).
checknight(NI,NI):-
    NI \==  0.

list_length([], 0).
list_length([_|T], N):-
    list_length(T, N1),
    N is N1 + 1.



xleh kompile

[Image: ehehehmprolog.png]

Uploaded with ImageShack.us

semua singleton error