Prolog - Write out facts and reading a users input











up vote
2
down vote

favorite












I am quite new to Prolog and have had some trouble understanding it.
I have some facts named 'problem' I wish to first print out these facts to the user and then ask them to input a value, this value is then read and used later.



From my understanding thus far, it would be best to use a forall to print out these facts and then use read to read the value inputted, but I am having some issue implementing this. Here is what I have so far, any explanation would be appreciated



My question: How do I read in the input from the user regarding the problem and apply that into a variable for later use?



tellMeYourProblem:-
forall(problem(P),
writeln(P)),
answer = read(X),


problem('1').
problem('2').
problem('3').
problem('4').
problem('5').
problem('6').
problem('7').
problem('8').
problem('9').
problem('10').









share|improve this question
























  • Of interest: Read–eval–print loop
    – Guy Coder
    Nov 15 at 13:30










  • Of interest: RosettaCode Prolog Input/Output for Lines of Text
    – Guy Coder
    Nov 15 at 13:34










  • ProblemAnswer(X) looks like a predicate, but predicates do not start with an upper case letter, neither should TellMeYourProblem Also, where is the code to implement ProblemAnswer(X).
    – Guy Coder
    Nov 15 at 13:38












  • Your question is not very clear and this is turning into a discussion. Please ask a more specific question.
    – Guy Coder
    Nov 15 at 13:51










  • Since you note and your code shows that you are very new to Prolog perhaps you should read and practice the exercises at Learn Prolog Now!
    – Guy Coder
    Nov 15 at 13:52















up vote
2
down vote

favorite












I am quite new to Prolog and have had some trouble understanding it.
I have some facts named 'problem' I wish to first print out these facts to the user and then ask them to input a value, this value is then read and used later.



From my understanding thus far, it would be best to use a forall to print out these facts and then use read to read the value inputted, but I am having some issue implementing this. Here is what I have so far, any explanation would be appreciated



My question: How do I read in the input from the user regarding the problem and apply that into a variable for later use?



tellMeYourProblem:-
forall(problem(P),
writeln(P)),
answer = read(X),


problem('1').
problem('2').
problem('3').
problem('4').
problem('5').
problem('6').
problem('7').
problem('8').
problem('9').
problem('10').









share|improve this question
























  • Of interest: Read–eval–print loop
    – Guy Coder
    Nov 15 at 13:30










  • Of interest: RosettaCode Prolog Input/Output for Lines of Text
    – Guy Coder
    Nov 15 at 13:34










  • ProblemAnswer(X) looks like a predicate, but predicates do not start with an upper case letter, neither should TellMeYourProblem Also, where is the code to implement ProblemAnswer(X).
    – Guy Coder
    Nov 15 at 13:38












  • Your question is not very clear and this is turning into a discussion. Please ask a more specific question.
    – Guy Coder
    Nov 15 at 13:51










  • Since you note and your code shows that you are very new to Prolog perhaps you should read and practice the exercises at Learn Prolog Now!
    – Guy Coder
    Nov 15 at 13:52













up vote
2
down vote

favorite









up vote
2
down vote

favorite











I am quite new to Prolog and have had some trouble understanding it.
I have some facts named 'problem' I wish to first print out these facts to the user and then ask them to input a value, this value is then read and used later.



From my understanding thus far, it would be best to use a forall to print out these facts and then use read to read the value inputted, but I am having some issue implementing this. Here is what I have so far, any explanation would be appreciated



My question: How do I read in the input from the user regarding the problem and apply that into a variable for later use?



tellMeYourProblem:-
forall(problem(P),
writeln(P)),
answer = read(X),


problem('1').
problem('2').
problem('3').
problem('4').
problem('5').
problem('6').
problem('7').
problem('8').
problem('9').
problem('10').









share|improve this question















I am quite new to Prolog and have had some trouble understanding it.
I have some facts named 'problem' I wish to first print out these facts to the user and then ask them to input a value, this value is then read and used later.



From my understanding thus far, it would be best to use a forall to print out these facts and then use read to read the value inputted, but I am having some issue implementing this. Here is what I have so far, any explanation would be appreciated



My question: How do I read in the input from the user regarding the problem and apply that into a variable for later use?



tellMeYourProblem:-
forall(problem(P),
writeln(P)),
answer = read(X),


problem('1').
problem('2').
problem('3').
problem('4').
problem('5').
problem('6').
problem('7').
problem('8').
problem('9').
problem('10').






prolog artificial-intelligence






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 15 at 13:53

























asked Nov 15 at 13:24









Joe G

697




697












  • Of interest: Read–eval–print loop
    – Guy Coder
    Nov 15 at 13:30










  • Of interest: RosettaCode Prolog Input/Output for Lines of Text
    – Guy Coder
    Nov 15 at 13:34










  • ProblemAnswer(X) looks like a predicate, but predicates do not start with an upper case letter, neither should TellMeYourProblem Also, where is the code to implement ProblemAnswer(X).
    – Guy Coder
    Nov 15 at 13:38












  • Your question is not very clear and this is turning into a discussion. Please ask a more specific question.
    – Guy Coder
    Nov 15 at 13:51










  • Since you note and your code shows that you are very new to Prolog perhaps you should read and practice the exercises at Learn Prolog Now!
    – Guy Coder
    Nov 15 at 13:52


















  • Of interest: Read–eval–print loop
    – Guy Coder
    Nov 15 at 13:30










  • Of interest: RosettaCode Prolog Input/Output for Lines of Text
    – Guy Coder
    Nov 15 at 13:34










  • ProblemAnswer(X) looks like a predicate, but predicates do not start with an upper case letter, neither should TellMeYourProblem Also, where is the code to implement ProblemAnswer(X).
    – Guy Coder
    Nov 15 at 13:38












  • Your question is not very clear and this is turning into a discussion. Please ask a more specific question.
    – Guy Coder
    Nov 15 at 13:51










  • Since you note and your code shows that you are very new to Prolog perhaps you should read and practice the exercises at Learn Prolog Now!
    – Guy Coder
    Nov 15 at 13:52
















Of interest: Read–eval–print loop
– Guy Coder
Nov 15 at 13:30




Of interest: Read–eval–print loop
– Guy Coder
Nov 15 at 13:30












Of interest: RosettaCode Prolog Input/Output for Lines of Text
– Guy Coder
Nov 15 at 13:34




Of interest: RosettaCode Prolog Input/Output for Lines of Text
– Guy Coder
Nov 15 at 13:34












ProblemAnswer(X) looks like a predicate, but predicates do not start with an upper case letter, neither should TellMeYourProblem Also, where is the code to implement ProblemAnswer(X).
– Guy Coder
Nov 15 at 13:38






ProblemAnswer(X) looks like a predicate, but predicates do not start with an upper case letter, neither should TellMeYourProblem Also, where is the code to implement ProblemAnswer(X).
– Guy Coder
Nov 15 at 13:38














Your question is not very clear and this is turning into a discussion. Please ask a more specific question.
– Guy Coder
Nov 15 at 13:51




Your question is not very clear and this is turning into a discussion. Please ask a more specific question.
– Guy Coder
Nov 15 at 13:51












Since you note and your code shows that you are very new to Prolog perhaps you should read and practice the exercises at Learn Prolog Now!
– Guy Coder
Nov 15 at 13:52




Since you note and your code shows that you are very new to Prolog perhaps you should read and practice the exercises at Learn Prolog Now!
– Guy Coder
Nov 15 at 13:52












2 Answers
2






active

oldest

votes

















up vote
1
down vote



accepted










Note: This answer uses SWI-Prolog.




How do I read in the input from the user regarding the problem?




You are doing that already with read(X), however read/1 reads terms (terms end with periods) and you probably want to read characters. If you are using SWI-Prolog take a look at Primitive character I/O




How do I apply that into a variable for later use?




When doing basic I/O with a user at a text level, a REPL is a good way to start. Adding a REPL is a bit more complicated so I will give you the code.



tellMeYourProblem:-
output_problems,
read_input.

output_problems :-
forall(problem(P),
writeln(P)).

read_input :-
repeat,
read_string(user_input, "n", "rt ", _, Line),
process_input(Line).

process_input(Line) :-
string(Line),
atom_number(Line, N),
integer(N),
do_something_with(Line),
fail.

process_input("quit") :-
write('Finished'), nl,
!, true.

do_something_with(X) :-
writeln(X).


problem('1').
problem('2').
problem('3').
problem('4').
problem('5').
problem('6').
problem('7').
problem('8').
problem('9').
problem('10').


Also with Prolog, the style is to use snake casing so tellMeYourProblem should be changed.



Normally in Prolog a REPL is done with ->/2, (Read Input till quit statement Prolog) , but I changed this to add more guard statements so that the exit condition would work, e.g.



string(Line),
atom_number(Line, N),
integer(N)


or putting the guard in the head, e.g.



process_input("quit")


When doing I/O to a screen and keyboard, the thought is to use stdIn and stdOut but for the keyboard SWI-Prolog uses user_input instead. See: Input and output



After all of the boiler plate code for the REPL is the next part you seek which is to do something with the input value, in this case just print it out.



do_something_with(X) :-
writeln(X).





share|improve this answer






























    up vote
    0
    down vote













    The easiest to write out the facts of problem/1,
    is to use the builtin listing/[0,1]. This builtin
    accepts a so called predicate indicator. You can
    write out the facts via:



    ?- listing(problem/1).


    The predicate is supported by many Prolog systems
    such as GNU Prolog, etc.. For how to read input see
    for example the post by Guy Coder.






    share|improve this answer





















      Your Answer






      StackExchange.ifUsing("editor", function () {
      StackExchange.using("externalEditor", function () {
      StackExchange.using("snippets", function () {
      StackExchange.snippets.init();
      });
      });
      }, "code-snippets");

      StackExchange.ready(function() {
      var channelOptions = {
      tags: "".split(" "),
      id: "1"
      };
      initTagRenderer("".split(" "), "".split(" "), channelOptions);

      StackExchange.using("externalEditor", function() {
      // Have to fire editor after snippets, if snippets enabled
      if (StackExchange.settings.snippets.snippetsEnabled) {
      StackExchange.using("snippets", function() {
      createEditor();
      });
      }
      else {
      createEditor();
      }
      });

      function createEditor() {
      StackExchange.prepareEditor({
      heartbeatType: 'answer',
      convertImagesToLinks: true,
      noModals: true,
      showLowRepImageUploadWarning: true,
      reputationToPostImages: 10,
      bindNavPrevention: true,
      postfix: "",
      imageUploader: {
      brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
      contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
      allowUrls: true
      },
      onDemand: true,
      discardSelector: ".discard-answer"
      ,immediatelyShowMarkdownHelp:true
      });


      }
      });














      draft saved

      draft discarded


















      StackExchange.ready(
      function () {
      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53320491%2fprolog-write-out-facts-and-reading-a-users-input%23new-answer', 'question_page');
      }
      );

      Post as a guest















      Required, but never shown

























      2 Answers
      2






      active

      oldest

      votes








      2 Answers
      2






      active

      oldest

      votes









      active

      oldest

      votes






      active

      oldest

      votes








      up vote
      1
      down vote



      accepted










      Note: This answer uses SWI-Prolog.




      How do I read in the input from the user regarding the problem?




      You are doing that already with read(X), however read/1 reads terms (terms end with periods) and you probably want to read characters. If you are using SWI-Prolog take a look at Primitive character I/O




      How do I apply that into a variable for later use?




      When doing basic I/O with a user at a text level, a REPL is a good way to start. Adding a REPL is a bit more complicated so I will give you the code.



      tellMeYourProblem:-
      output_problems,
      read_input.

      output_problems :-
      forall(problem(P),
      writeln(P)).

      read_input :-
      repeat,
      read_string(user_input, "n", "rt ", _, Line),
      process_input(Line).

      process_input(Line) :-
      string(Line),
      atom_number(Line, N),
      integer(N),
      do_something_with(Line),
      fail.

      process_input("quit") :-
      write('Finished'), nl,
      !, true.

      do_something_with(X) :-
      writeln(X).


      problem('1').
      problem('2').
      problem('3').
      problem('4').
      problem('5').
      problem('6').
      problem('7').
      problem('8').
      problem('9').
      problem('10').


      Also with Prolog, the style is to use snake casing so tellMeYourProblem should be changed.



      Normally in Prolog a REPL is done with ->/2, (Read Input till quit statement Prolog) , but I changed this to add more guard statements so that the exit condition would work, e.g.



      string(Line),
      atom_number(Line, N),
      integer(N)


      or putting the guard in the head, e.g.



      process_input("quit")


      When doing I/O to a screen and keyboard, the thought is to use stdIn and stdOut but for the keyboard SWI-Prolog uses user_input instead. See: Input and output



      After all of the boiler plate code for the REPL is the next part you seek which is to do something with the input value, in this case just print it out.



      do_something_with(X) :-
      writeln(X).





      share|improve this answer



























        up vote
        1
        down vote



        accepted










        Note: This answer uses SWI-Prolog.




        How do I read in the input from the user regarding the problem?




        You are doing that already with read(X), however read/1 reads terms (terms end with periods) and you probably want to read characters. If you are using SWI-Prolog take a look at Primitive character I/O




        How do I apply that into a variable for later use?




        When doing basic I/O with a user at a text level, a REPL is a good way to start. Adding a REPL is a bit more complicated so I will give you the code.



        tellMeYourProblem:-
        output_problems,
        read_input.

        output_problems :-
        forall(problem(P),
        writeln(P)).

        read_input :-
        repeat,
        read_string(user_input, "n", "rt ", _, Line),
        process_input(Line).

        process_input(Line) :-
        string(Line),
        atom_number(Line, N),
        integer(N),
        do_something_with(Line),
        fail.

        process_input("quit") :-
        write('Finished'), nl,
        !, true.

        do_something_with(X) :-
        writeln(X).


        problem('1').
        problem('2').
        problem('3').
        problem('4').
        problem('5').
        problem('6').
        problem('7').
        problem('8').
        problem('9').
        problem('10').


        Also with Prolog, the style is to use snake casing so tellMeYourProblem should be changed.



        Normally in Prolog a REPL is done with ->/2, (Read Input till quit statement Prolog) , but I changed this to add more guard statements so that the exit condition would work, e.g.



        string(Line),
        atom_number(Line, N),
        integer(N)


        or putting the guard in the head, e.g.



        process_input("quit")


        When doing I/O to a screen and keyboard, the thought is to use stdIn and stdOut but for the keyboard SWI-Prolog uses user_input instead. See: Input and output



        After all of the boiler plate code for the REPL is the next part you seek which is to do something with the input value, in this case just print it out.



        do_something_with(X) :-
        writeln(X).





        share|improve this answer

























          up vote
          1
          down vote



          accepted







          up vote
          1
          down vote



          accepted






          Note: This answer uses SWI-Prolog.




          How do I read in the input from the user regarding the problem?




          You are doing that already with read(X), however read/1 reads terms (terms end with periods) and you probably want to read characters. If you are using SWI-Prolog take a look at Primitive character I/O




          How do I apply that into a variable for later use?




          When doing basic I/O with a user at a text level, a REPL is a good way to start. Adding a REPL is a bit more complicated so I will give you the code.



          tellMeYourProblem:-
          output_problems,
          read_input.

          output_problems :-
          forall(problem(P),
          writeln(P)).

          read_input :-
          repeat,
          read_string(user_input, "n", "rt ", _, Line),
          process_input(Line).

          process_input(Line) :-
          string(Line),
          atom_number(Line, N),
          integer(N),
          do_something_with(Line),
          fail.

          process_input("quit") :-
          write('Finished'), nl,
          !, true.

          do_something_with(X) :-
          writeln(X).


          problem('1').
          problem('2').
          problem('3').
          problem('4').
          problem('5').
          problem('6').
          problem('7').
          problem('8').
          problem('9').
          problem('10').


          Also with Prolog, the style is to use snake casing so tellMeYourProblem should be changed.



          Normally in Prolog a REPL is done with ->/2, (Read Input till quit statement Prolog) , but I changed this to add more guard statements so that the exit condition would work, e.g.



          string(Line),
          atom_number(Line, N),
          integer(N)


          or putting the guard in the head, e.g.



          process_input("quit")


          When doing I/O to a screen and keyboard, the thought is to use stdIn and stdOut but for the keyboard SWI-Prolog uses user_input instead. See: Input and output



          After all of the boiler plate code for the REPL is the next part you seek which is to do something with the input value, in this case just print it out.



          do_something_with(X) :-
          writeln(X).





          share|improve this answer














          Note: This answer uses SWI-Prolog.




          How do I read in the input from the user regarding the problem?




          You are doing that already with read(X), however read/1 reads terms (terms end with periods) and you probably want to read characters. If you are using SWI-Prolog take a look at Primitive character I/O




          How do I apply that into a variable for later use?




          When doing basic I/O with a user at a text level, a REPL is a good way to start. Adding a REPL is a bit more complicated so I will give you the code.



          tellMeYourProblem:-
          output_problems,
          read_input.

          output_problems :-
          forall(problem(P),
          writeln(P)).

          read_input :-
          repeat,
          read_string(user_input, "n", "rt ", _, Line),
          process_input(Line).

          process_input(Line) :-
          string(Line),
          atom_number(Line, N),
          integer(N),
          do_something_with(Line),
          fail.

          process_input("quit") :-
          write('Finished'), nl,
          !, true.

          do_something_with(X) :-
          writeln(X).


          problem('1').
          problem('2').
          problem('3').
          problem('4').
          problem('5').
          problem('6').
          problem('7').
          problem('8').
          problem('9').
          problem('10').


          Also with Prolog, the style is to use snake casing so tellMeYourProblem should be changed.



          Normally in Prolog a REPL is done with ->/2, (Read Input till quit statement Prolog) , but I changed this to add more guard statements so that the exit condition would work, e.g.



          string(Line),
          atom_number(Line, N),
          integer(N)


          or putting the guard in the head, e.g.



          process_input("quit")


          When doing I/O to a screen and keyboard, the thought is to use stdIn and stdOut but for the keyboard SWI-Prolog uses user_input instead. See: Input and output



          After all of the boiler plate code for the REPL is the next part you seek which is to do something with the input value, in this case just print it out.



          do_something_with(X) :-
          writeln(X).






          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited Nov 15 at 15:36

























          answered Nov 15 at 15:18









          Guy Coder

          14.5k43781




          14.5k43781
























              up vote
              0
              down vote













              The easiest to write out the facts of problem/1,
              is to use the builtin listing/[0,1]. This builtin
              accepts a so called predicate indicator. You can
              write out the facts via:



              ?- listing(problem/1).


              The predicate is supported by many Prolog systems
              such as GNU Prolog, etc.. For how to read input see
              for example the post by Guy Coder.






              share|improve this answer

























                up vote
                0
                down vote













                The easiest to write out the facts of problem/1,
                is to use the builtin listing/[0,1]. This builtin
                accepts a so called predicate indicator. You can
                write out the facts via:



                ?- listing(problem/1).


                The predicate is supported by many Prolog systems
                such as GNU Prolog, etc.. For how to read input see
                for example the post by Guy Coder.






                share|improve this answer























                  up vote
                  0
                  down vote










                  up vote
                  0
                  down vote









                  The easiest to write out the facts of problem/1,
                  is to use the builtin listing/[0,1]. This builtin
                  accepts a so called predicate indicator. You can
                  write out the facts via:



                  ?- listing(problem/1).


                  The predicate is supported by many Prolog systems
                  such as GNU Prolog, etc.. For how to read input see
                  for example the post by Guy Coder.






                  share|improve this answer












                  The easiest to write out the facts of problem/1,
                  is to use the builtin listing/[0,1]. This builtin
                  accepts a so called predicate indicator. You can
                  write out the facts via:



                  ?- listing(problem/1).


                  The predicate is supported by many Prolog systems
                  such as GNU Prolog, etc.. For how to read input see
                  for example the post by Guy Coder.







                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered Nov 15 at 17:26









                  Harry Stoteles

                  292




                  292






























                      draft saved

                      draft discarded




















































                      Thanks for contributing an answer to Stack Overflow!


                      • Please be sure to answer the question. Provide details and share your research!

                      But avoid



                      • Asking for help, clarification, or responding to other answers.

                      • Making statements based on opinion; back them up with references or personal experience.


                      To learn more, see our tips on writing great answers.





                      Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


                      Please pay close attention to the following guidance:


                      • Please be sure to answer the question. Provide details and share your research!

                      But avoid



                      • Asking for help, clarification, or responding to other answers.

                      • Making statements based on opinion; back them up with references or personal experience.


                      To learn more, see our tips on writing great answers.




                      draft saved


                      draft discarded














                      StackExchange.ready(
                      function () {
                      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53320491%2fprolog-write-out-facts-and-reading-a-users-input%23new-answer', 'question_page');
                      }
                      );

                      Post as a guest















                      Required, but never shown





















































                      Required, but never shown














                      Required, but never shown












                      Required, but never shown







                      Required, but never shown

































                      Required, but never shown














                      Required, but never shown












                      Required, but never shown







                      Required, but never shown







                      Popular posts from this blog

                      How to send String Array data to Server using php in android

                      Title Spacing in Bjornstrup Chapter, Removing Chapter Number From Contents

                      Is anime1.com a legal site for watching anime?