Prolog - Write out facts and reading a users input










3















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 '18 at 13:30











  • Of interest: RosettaCode Prolog Input/Output for Lines of Text

    – Guy Coder
    Nov 15 '18 at 13:34











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

    – VicentVega
    Nov 15 '18 at 13:53















3















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 '18 at 13:30











  • Of interest: RosettaCode Prolog Input/Output for Lines of Text

    – Guy Coder
    Nov 15 '18 at 13:34











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

    – VicentVega
    Nov 15 '18 at 13:53













3












3








3








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






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Feb 14 at 20:59









false

10.2k773150




10.2k773150










asked Nov 15 '18 at 13:24









VicentVegaVicentVega

9618




9618












  • Of interest: Read–eval–print loop

    – Guy Coder
    Nov 15 '18 at 13:30











  • Of interest: RosettaCode Prolog Input/Output for Lines of Text

    – Guy Coder
    Nov 15 '18 at 13:34











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

    – VicentVega
    Nov 15 '18 at 13:53

















  • Of interest: Read–eval–print loop

    – Guy Coder
    Nov 15 '18 at 13:30











  • Of interest: RosettaCode Prolog Input/Output for Lines of Text

    – Guy Coder
    Nov 15 '18 at 13:34











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

    – VicentVega
    Nov 15 '18 at 13:53
















Of interest: Read–eval–print loop

– Guy Coder
Nov 15 '18 at 13:30





Of interest: Read–eval–print loop

– Guy Coder
Nov 15 '18 at 13:30













Of interest: RosettaCode Prolog Input/Output for Lines of Text

– Guy Coder
Nov 15 '18 at 13:34





Of interest: RosettaCode Prolog Input/Output for Lines of Text

– Guy Coder
Nov 15 '18 at 13:34













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

– VicentVega
Nov 15 '18 at 13:53





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

– VicentVega
Nov 15 '18 at 13:53












2 Answers
2






active

oldest

votes


















3














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 for reading characters and Predicates that operate on strings for reading strings.




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



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
































    0














    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',
      autoActivateHeartbeat: false,
      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









      3














      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 for reading characters and Predicates that operate on strings for reading strings.




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



      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





























        3














        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 for reading characters and Predicates that operate on strings for reading strings.




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



        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



























          3












          3








          3







          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 for reading characters and Predicates that operate on strings for reading strings.




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



          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 for reading characters and Predicates that operate on strings for reading strings.




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



          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 Feb 14 at 20:44









          repeat

          16.2k443139




          16.2k443139










          answered Nov 15 '18 at 15:18









          Guy CoderGuy Coder

          16.1k44287




          16.1k44287























              0














              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



























                0














                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

























                  0












                  0








                  0







                  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 '18 at 17:26









                  Harry StotelesHarry Stoteles

                  293




                  293



























                      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.




                      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







                      這個網誌中的熱門文章

                      How to read a connectionString WITH PROVIDER in .NET Core?

                      Node.js Script on GitHub Pages or Amazon S3

                      Museum of Modern and Contemporary Art of Trento and Rovereto