I am trying to get a list of all rooms that are available by using bagof:
%roomdb(roomnumber,roomtype,shower,price,freetonight,freetomorrow).
roomdb(1,double,no,35,roomfree,roomfree).
roomdb(2,double,yes,40,roomfree,roomfree).
roomdb(3,single,yes,25,roomfree,roomfree).
roomdb(4,single,no,20,roomfree,roomfree).
roomdb(5,single,yes,25,roomfree,roomfree).
printlist([H|T]):-
write(H), nl,
printlist(T).
listrooms(1):-
bagof(RMNUMBER,roomdb(RMNUMBER,_,_,_,roomfree,_), Rooms),
list_length(Rooms,L),
%for testing
write('list length '), write(L), nl,
printlist(Rooms).
listrooms(2):-
bagof(RMNUMBER,roomdb(RMNUMBER,_,_,_,_,roomfree), Rooms),
list_length(Rooms,L),
%for testing
write('list length '), write(L), nl,
printlist(Rooms).
listrooms(NI):-
night1(NI,[NIGHT,_],L),
write('no rooms are free '),
write(NIGHT), nl.
The output I get from this is:
hotel>>|: which rooms are free tonight.
list length 1
4
list length 2
3
5
list length 1
1
list length 1
2
no rooms are free tonight
I thought that bagof would give me one list of 5 numbers (for all of the room numbers) but instead it gives me a different list each time it is tried, but never uses the same numbers in these different lists. If I try this:
| ?- roomdb(Room,_,_,_,roomfree,_).
Room = 1 ;
Room = 2 ;
Room = 3 ;
Room = 4 ;
Room = 5
I do get all 5 numbers so it obviously succeeds 5 times so why doesnj't bagof give me a 5 item list. Why isn't this working?
Thanks, Giles
Giles Roadnight
messenger: giles_roadnight@hotmail.com
ICQ: 81621370
%roomdb(roomnumber,roomtype,shower,price,freetonight,freetomorrow).
roomdb(1,double,no,35,roomfree,roomfree).
roomdb(2,double,yes,40,roomfree,roomfree).
roomdb(3,single,yes,25,roomfree,roomfree).
roomdb(4,single,no,20,roomfree,roomfree).
roomdb(5,single,yes,25,roomfree,roomfree).
printlist([H|T]):-
write(H), nl,
printlist(T).
listrooms(1):-
bagof(RMNUMBER,roomdb(RMNUMBER,_,_,_,roomfree,_), Rooms),
list_length(Rooms,L),
%for testing
write('list length '), write(L), nl,
printlist(Rooms).
listrooms(2):-
bagof(RMNUMBER,roomdb(RMNUMBER,_,_,_,_,roomfree), Rooms),
list_length(Rooms,L),
%for testing
write('list length '), write(L), nl,
printlist(Rooms).
listrooms(NI):-
night1(NI,[NIGHT,_],L),
write('no rooms are free '),
write(NIGHT), nl.
The output I get from this is:
hotel>>|: which rooms are free tonight.
list length 1
4
list length 2
3
5
list length 1
1
list length 1
2
no rooms are free tonight
I thought that bagof would give me one list of 5 numbers (for all of the room numbers) but instead it gives me a different list each time it is tried, but never uses the same numbers in these different lists. If I try this:
| ?- roomdb(Room,_,_,_,roomfree,_).
Room = 1 ;
Room = 2 ;
Room = 3 ;
Room = 4 ;
Room = 5
I do get all 5 numbers so it obviously succeeds 5 times so why doesnj't bagof give me a 5 item list. Why isn't this working?
Thanks, Giles
Giles Roadnight
messenger: giles_roadnight@hotmail.com
ICQ: 81621370