1
votes

Hi i'm new to Prolog and I think i'm having trouble with the binding of variables. The question that im working off is this.

and this is what I have come up with so far:

:- dynamic lecture/1.

%teacherNames(['Paul'],[['Bsc4'],['Bsc1']], [['AI'],['Games Design']]).
teacherNames(['Paul'],['Bsc4'],['AI']).
teacherNames(['Paul'],['Bsc1'],['Games_dev']).

group(['Hsoftware'],['Bsc4'],['15']).
group(['total'],['Bsc1'],['127']).
group(['games'],['Bsc1'],['52']).
group(['software'],['Bsc1'],['35']).
group(['web'],['Bsc1'],['15']).
group(['systems'],['Bsc1'],['25']).

room(['B1041'],['32']).
room(['B2008'],['32']).
room(['A0006'],['140']).

student(['Bill Bloggs'],['s00000123'],['Bsc4']).
student(['Andera Martin'],['s00000100'],['software']).

assignRoom:-
    %retract(lecture(TeachersName,TeachersGroup,TeachSubject)),

write('teachers name?'),nl,
read(TeachersName),
%teacherNames(TeachersName),nl,
write('Group for class'),nl,
read(TeachersGroup),

%write('Subject?'),nl,
%read(TeachSubject),
teacherNames(TeachersName,TeachersGroup,TeachSubject),

%GroupName == TeachersGroup,
%group(GroupName,_,_),

    write('Choose Stream'),nl,
read(Stream),

write('time period for class'),nl,
read(Period),

group(Stream,_,_),

write('choose room'),nl,
read(Room),
room(Room,_),
    %%assertz(lecture(GroupName,Subject)).
    asserta(lecture(TeachersName,TeachersGroup,TeachSubject,Period,Room)).

my problem is that I want it that 'Paul' will only be able to teach AI to 'Bsc4' and only able to teach 'Games_dev' to 'Bsc1'. So in this case if I were to input:

assignRoom.
Paul %teachers name
Bsc4 %the group that will be going to the class
Hsoftware %the stream name
1 %the time the class will be on
B1041 %the room the class will be held in

That works but if i try to put in anything other than those values it fails. How I want it to work is that if the Group is 'Bsc4' than only Hsoftware will be true and if Group name was 'Bsc1' Hsoftware would be false while software,web etc. will be true and the appropriate values will be bound to lecture.

Edit:

I changed around somethings and what i got is working alot better if i don't take user error into account (which will do for this example). the new code is in this link the problem i'm having now is the binding of the Room variable..

if i input

 assignRoom.
 Paul.
 Bsc1.
 web.
 1.
 A0006.

I get the results I want apart from the RoomNumber always outputs as

 lecture(X,C,V,B,N,Y).
 Paul %correct
 Bsc1 %correct
 web %correct
 games_dev %correct
 1 %correct
 B1041 %Should be A0006

So what i'm asking now would be how do I fix the binding for the room number.

1
Is there a specific reason you're using a list construct for all of your data components? E.g., why is it room(['B1041'],['32']). and not room('B1041', 32)? Anything that is a single value doesn't need to be a list. You only need a list of you require the flexibility of allowing zero, one, or more elements.lurker
honestly I didn't even know there was a difference I just thought that is what the syntax was.prolog noob
It's fundamental Prolog syntax. You might want to review some introductory Prolog tutorial or get a good introductory Prolog book before tackling this program.lurker
unfortunately i'm really short on time and finding all this Prolog stuff very confusing lolprolog noob
What are the criteria to select the room? In the current form, only the first fact room will be unified.Yasel

1 Answers

0
votes

The idea is that the room should be explicitly selected, so my suggestion is to show the user what their options are. First, create a predicate that will print all rooms:

print_rooms:- room(N, C), 
              write(N), write(' - '),write(C), write(', '),
              fail.
print_rooms.

Second, use this predicate before ask the user to select the room:

...
write('choose room: '), print_rooms ,nl,
read(Room),
room(Room,_),
...

You can do this much better and cleaner, but this may work. Other thing you must do is keeping asking the user for the room, until he/she select a valid option.