{"id":3818,"date":"2024-09-07T18:31:05","date_gmt":"2024-09-07T18:31:05","guid":{"rendered":"https:\/\/workhouse.sweetdishy.com\/?p=3818"},"modified":"2024-09-07T18:31:05","modified_gmt":"2024-09-07T18:31:05","slug":"handling-input-and-output","status":"publish","type":"post","link":"https:\/\/workhouse.sweetdishy.com\/index.php\/2024\/09\/07\/handling-input-and-output\/","title":{"rendered":"Handling Input and Output"},"content":{"rendered":"\n<p id=\"Arti00012005\">Till now, we have executed queries on the console. But many-a-times, it is necessary to some information from the user, process it and then display the result on the console. For such applications, we must know how to perform input-output operations in Prolog.<\/p>\n\n\n\n<h4 class=\"wp-block-heading\" id=\"Arti00012006\"><strong>The&nbsp;write()&nbsp;Predicate<\/strong><\/h4>\n\n\n\n<p id=\"Arti00012007\">As the name suggests, the&nbsp;<strong>write()<\/strong>&nbsp;predicate takes the parameter as input, and writes the content into the console (by default). The content can also be written in files. Look at figure which demonstrates the usage of&nbsp;<strong>write()<\/strong>&nbsp;predicate.<\/p>\n\n\n\n<p id=\"Arti00012008\">Note that we can insert a new line in the output displayed by writing \u2019nl\u2019. From the figure, it is clear that to print a string on the console, we have to use single quotes (\u2018string\u2019). If we use double quote (\u201cstring\u201d), then it will return a list of ASCII values.<\/p>\n\n\n\n<h4 class=\"wp-block-heading\" id=\"Arti00012009\"><strong>The&nbsp;read()&nbsp;Predicate<\/strong><\/h4>\n\n\n\n<p id=\"Arti00012010\">The&nbsp;<strong>read()<\/strong>&nbsp;predicate is used to read some information from the user through the console. Users can type some content in the console, that can be taken as input and further processed. However, it is not mandatory that Prolog programs can read only from console. The programs can also read data from files.<\/p>\n\n\n\n<p id=\"Arti00012011\">Figure shows a simple program that reads a number from the user and displays it square.<\/p>\n\n\n\n<p><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/learning.oreilly.com\/api\/v2\/epubs\/urn:orm:book:9789357053778\/files\/images\/img_p585-1.png\" alt=\"images\" width=\"369\" height=\"168\"><\/p>\n\n\n\n<p><\/p>\n\n\n\n<p id=\"Arti00012015\">&nbsp;<\/p>\n\n\n\n<p id=\"Arti00012016\">In Figure, we have extended this functionality by writing a program in a .pl file and then executing it in console. The program keeps reading number from user and displays its square until the user enters a \u2018.\u2019 (full stop).<\/p>\n\n\n\n<p><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/learning.oreilly.com\/api\/v2\/epubs\/urn:orm:book:9789357053778\/files\/images\/img_p585-2.png\" alt=\"images\" width=\"245\" height=\"338\"><\/p>\n\n\n\n<p><\/p>\n\n\n\n<p><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/learning.oreilly.com\/api\/v2\/epubs\/urn:orm:book:9789357053778\/files\/images\/img_p585-3.png\" alt=\"images\" width=\"236\" height=\"381\"><\/p>\n\n\n\n<p><\/p>\n\n\n\n<p><a><\/a><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/learning.oreilly.com\/api\/v2\/epubs\/urn:orm:book:9789357053778\/files\/images\/img_p586-1.png\" alt=\"images\" width=\"569\" height=\"297\"><\/p>\n\n\n\n<p><\/p>\n\n\n\n<h4 class=\"wp-block-heading\" id=\"Arti00012026\"><strong>The&nbsp;tab()&nbsp;Predicate<\/strong><\/h4>\n\n\n\n<p id=\"Arti00012027\">The&nbsp;<strong>tab()<\/strong>&nbsp;predicate is used to put some blank-spaces while we displaying some content. The predicate takes a number as an argument and prints those many numbers of blank spaces. For example, to print 5 blank spaces between Good and Morning write the following lines.<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/learning.oreilly.com\/api\/v2\/epubs\/urn:orm:book:9789357053778\/files\/images\/img_p586-2.png\" alt=\"images\"\/><\/figure>\n\n\n\n<p id=\"Arti00012030\">&nbsp;<\/p>\n\n\n\n<p id=\"Arti00012032\">We can open more than one file using tell(). When told is called, all files will be closed.<\/p>\n\n\n\n<h4 class=\"wp-block-heading\" id=\"Arti00012033\"><strong>Reading\/Writing Files<\/strong><\/h4>\n\n\n\n<p id=\"Arti00012034\">Prolog has some built-in predicates, that can be used to read from file and write into it. In this section, we will read about them.<\/p>\n\n\n\n<h4 class=\"wp-block-heading\" id=\"Arti00012035\"><strong>The tell and told<\/strong><\/h4>\n\n\n\n<p id=\"Arti00012036\">The&nbsp;<strong>tell()<\/strong>&nbsp;predicate is used to write data into a file and not the console. For this, it accepts filename as the argument. If that file is not present, then a new file is created data is written into it. If the file already exists, it will be opened for writing. A file once opened using&nbsp;<strong>tell()<\/strong>&nbsp;will remain opened until we use the&nbsp;<strong>told()<\/strong>&nbsp;predicate.<\/p>\n\n\n\n<h4 class=\"wp-block-heading\" id=\"Arti00012037\"><strong>The see and seen<\/strong><\/h4>\n\n\n\n<p id=\"Arti00012038\">Like&nbsp;<strong>tell()<\/strong>&nbsp;and&nbsp;<strong>told()<\/strong>, Prolog has&nbsp;<strong>see()<\/strong>&nbsp;and&nbsp;<strong>seen()<\/strong>&nbsp;predicates that are used for reading information from file and not from the keyboard. The&nbsp;<strong>see()<\/strong>&nbsp;predicate therefore help us to change the current input stream. It takes the filename as input. Once the file is read and the read operation is completed, we will use&nbsp;<strong>seen()<\/strong>&nbsp;predicate. The&nbsp;<strong>seen()<\/strong>&nbsp;command transfers the control to the console again to allow accepting input from the console.<\/p>\n\n\n\n<p><a><\/a><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/learning.oreilly.com\/api\/v2\/epubs\/urn:orm:book:9789357053778\/files\/images\/img_p587-1.png\" alt=\"images\" width=\"343\" height=\"296\"><\/p>\n\n\n\n<p><em>Credit<\/em>: Copyright (C) 1999-2021 Daniel Diaz<\/p>\n\n\n\n<h4 class=\"wp-block-heading\" id=\"Arti00012042\"><strong>The&nbsp;put(C)&nbsp;and&nbsp;put_char(C)&nbsp;Predicates<\/strong><\/h4>\n\n\n\n<p id=\"Arti00012043\">The&nbsp;<strong>put(C)<\/strong>&nbsp;predicate is used to write a character, C at a time in the current output stream which can be either a file or the console.<\/p>\n\n\n\n<p id=\"Arti00012044\">The&nbsp;<strong>put_char(C)<\/strong>&nbsp;function does the same work but instead of accepting a character, it takes an ASCII code as an input.<\/p>\n\n\n\n<h4 class=\"wp-block-heading\" id=\"Arti00012045\"><strong>The&nbsp;get_char(C)&nbsp;and&nbsp;get_code(C)&nbsp;Predicates<\/strong><\/h4>\n\n\n\n<p id=\"Arti00012046\">Like&nbsp;<strong>put()<\/strong>&nbsp;and&nbsp;<strong>put_char()<\/strong>, we have&nbsp;<strong>get_char()<\/strong>&nbsp;and&nbsp;<strong>get_code()<\/strong>&nbsp;predicates in Prolog. As indicated by the name,&nbsp;<strong>get_char()<\/strong>&nbsp;reads a single character from the current input stream and the&nbsp;<strong>get_code(C)<\/strong>&nbsp;is used to return the ASCII value of the character, C.<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/learning.oreilly.com\/api\/v2\/epubs\/urn:orm:book:9789357053778\/files\/images\/img_p587-2.png\" alt=\"images\"\/><\/figure>\n","protected":false},"excerpt":{"rendered":"<p>Till now, we have executed queries on the console. But many-a-times, it is necessary to some information from the user, process it and then display the result on the console. For such applications, we must know how to perform input-output operations in Prolog. The&nbsp;write()&nbsp;Predicate As the name suggests, the&nbsp;write()&nbsp;predicate takes the parameter as input, and [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":3341,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[464],"tags":[],"class_list":["post-3818","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-programming-language-prolog"],"jetpack_featured_media_url":"https:\/\/workhouse.sweetdishy.com\/wp-content\/uploads\/2024\/08\/programming.png","_links":{"self":[{"href":"https:\/\/workhouse.sweetdishy.com\/index.php\/wp-json\/wp\/v2\/posts\/3818","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/workhouse.sweetdishy.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/workhouse.sweetdishy.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/workhouse.sweetdishy.com\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/workhouse.sweetdishy.com\/index.php\/wp-json\/wp\/v2\/comments?post=3818"}],"version-history":[{"count":1,"href":"https:\/\/workhouse.sweetdishy.com\/index.php\/wp-json\/wp\/v2\/posts\/3818\/revisions"}],"predecessor-version":[{"id":3819,"href":"https:\/\/workhouse.sweetdishy.com\/index.php\/wp-json\/wp\/v2\/posts\/3818\/revisions\/3819"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/workhouse.sweetdishy.com\/index.php\/wp-json\/wp\/v2\/media\/3341"}],"wp:attachment":[{"href":"https:\/\/workhouse.sweetdishy.com\/index.php\/wp-json\/wp\/v2\/media?parent=3818"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/workhouse.sweetdishy.com\/index.php\/wp-json\/wp\/v2\/categories?post=3818"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/workhouse.sweetdishy.com\/index.php\/wp-json\/wp\/v2\/tags?post=3818"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}