1

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'..

GoodGJ
  • 339
  • 1
  • 4
  • 17
  • 1
    You cannot make variables global in Prolog. See the answer I provided to your previous post here: http://stackoverflow.com/questions/20517560/i-cant-understand-the-result-of-my-code-in-prolog-programming/20518974#20518974. Your "expected atom bla bla..." error pertains to something different. – lurker Dec 11 '13 at 12:21
  • 2
    Please, if you get an error message, first read it carefully, then try to figure out what it means (read the documentation or google it), then, if this does not solve your problem, ask for help, **including relevant information**, for example the exact error message you get, and what did not solve it. Anything else is lack of respect for those willing to help. –  Dec 11 '13 at 12:55
  • @mbratch Strictly speaking, there are ways to make "global" variables, but there is seldom the need. In OP's example, definitely not. –  Dec 11 '13 at 12:58
  • @Boris You're surely right. I feel sorry for that. I got some clues from others...Next time, I will be careful about your advice. Thanks. – GoodGJ Dec 11 '13 at 13:00
  • @Boris, thanks, I hadn't thought of those completely. Please feel free to comment on my other answer to augment. – lurker Dec 11 '13 at 13:53

1 Answers1

2

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.

lurker
  • 56,987
  • 9
  • 69
  • 103
  • 1
    Wow. For me, this code looks like 'art'. : ) Thanks...!! and also for another questions. You gave me much help! – GoodGJ Dec 11 '13 at 16:32