Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

bagof 1

Status
Not open for further replies.

Roaders

Programmer
Aug 5, 2001
38
GB
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
 
Hello.
How about using "findall" instead of "bagof" ?
 
Thanks that works now pimi. Giles Roadnight
messenger: giles_roadnight@hotmail.com
ICQ: 81621370
 
Status
Not open for further replies.

Similar threads

Part and Inventory Search

Sponsor

Back
Top