Reputation: 339
there is some code as below
diagnosis :-
readln(Line1),
readln(Line2),
readln(Line3),
readln(Line4),
readln(Line5),
readln(Line6),
readln(Line7),
readln(Line8),
readln(Line9),
readln(Line10),
write(Line1),nl,
write(Line2),nl,
write(Line3),nl,
write(Line4),nl,
write(Line5),nl,
write(Line6),nl,
write(Line7),nl,
write(Line8),nl,
write(Line9),nl,
write(Line10),nl.
Here, I got 10 lines as list variables (Line1, ... Line10). The question is how I can make these variables to be global variable so that I can use it other predicates... I used something like b_setvalue or b_getvalue.. but errors occurred with the messages, 'expected atom bla bla'..
Upvotes: 1
Views: 1893
Reputation: 58244
A possible approach to reading in N diagnoses and collecting them could be:
% Read N diagnoses and retrieve them in a list
diagnosis(N, Diags) :-
diagnosis(N, [], D),
reverse(D, Diags). % Assuming you want them listed in the order they were read
diagnosis(N, A, Diags) :-
N > 0,
rw_diag(Diag), % read and write one diagnosis
A1 = [Diag|A],
N1 is N - 1,
diagnosis(N1, A1, Diags).
diagnosis(0, A, A).
rw_diag(Diag) :-
readln(Diag),
write(Diag), nl.
And then you would call diagnosis/2
as follows:
diagnosis(10, DiagList).
Which would provide you a prolog list of the diagnoses entered by the user. There are no global variables in use, but what you get is an instantiated list which you can use in whatever predicate calls diagnosis/2
.
This is just an example for instruction. There are probably a couple of different ways to do this, some perhaps simpler/better/different than what I've shown. One improvement might be not to hard code it to 10 inputs, but have the user input items one by one until they're done and collect an arbitrary length list of inputs.
Upvotes: 2