{"id":3816,"date":"2024-09-07T18:29:38","date_gmt":"2024-09-07T18:29:38","guid":{"rendered":"https:\/\/workhouse.sweetdishy.com\/?p=3816"},"modified":"2024-09-07T18:29:39","modified_gmt":"2024-09-07T18:29:39","slug":"miscellaneous-programs","status":"publish","type":"post","link":"https:\/\/workhouse.sweetdishy.com\/index.php\/2024\/09\/07\/miscellaneous-programs\/","title":{"rendered":"Miscellaneous Programs"},"content":{"rendered":"\n<p id=\"Arti00011974\"><strong>Program 1:<\/strong>&nbsp;Check whether the list has odd number of elements or the even number of elements.<\/p>\n\n\n\n<p id=\"Arti00011975\">To implement this functionality, we will define predicates list_even_len(L) and list_odd_len(L) while considering the following points.<\/p>\n\n\n\n<ul class=\"wp-block-list\" id=\"Arti00011976\">\n<li>An empty list has zero length. Thus, even length list.<\/li>\n\n\n\n<li>A list with a single element is an odd length list.<\/li>\n\n\n\n<li>Otherwise, a list [Head | Tail], has odd length if Tail is even length.<\/li>\n\n\n\n<li>A list [Head | Tail], has even length if Tail is odd length.<\/li>\n<\/ul>\n\n\n\n<p id=\"Arti00011981\">Write the following rules in kb5.pl and execute the commands as shown below.<\/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_p583.png\" alt=\"images\" width=\"559\" height=\"344\"><\/p>\n\n\n\n<p><em>Credit<\/em>: Copyright (C) 1999-2021 Daniel Diaz<\/p>\n\n\n\n<p id=\"Arti00011985\"><strong>Program 2:<\/strong>&nbsp;Find the maximum element from a list.<\/p>\n\n\n\n<p id=\"Arti00011986\">Let us define a predicate, list_max_elem(List, Max) considering that,<\/p>\n\n\n\n<ul class=\"wp-block-list\" id=\"Arti00011987\">\n<li>If there is only one element in the list, then Max will be that element itself.<\/li>\n\n\n\n<li>Else, divide the list as [X,Y|Tail]. Recursively find Max of [Y|Tail] and store it into MaxRest. Similarly, find maximum of X and MaxRest, and store it in Max.<\/li>\n<\/ul>\n\n\n\n<p id=\"Arti00011990\">To execute this predicate, first write the following rules in kb5.pl and then run the commands in console as shown below.<\/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_p583-2.png\" alt=\"images\"\/><\/figure>\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_p584-1.png\" alt=\"images\" width=\"571\" height=\"314\"><\/p>\n\n\n\n<p><em>Credit<\/em>: Copyright (C) 1999-2021 Daniel Diaz<\/p>\n\n\n\n<p id=\"Arti00011996\"><strong>Program 3:<\/strong>&nbsp;Find the sum of elements of a list.<\/p>\n\n\n\n<p id=\"Arti00011997\">Here, we will define a predicate, list_sum(List, Sum). If the list is empty, then sum will be 0. Else, list in the form of [Head|Tail], recursively finds sum of the tail and store them into SumTemp. Finally, sum is calculated as, Sum = Head + SumTemp.<\/p>\n\n\n\n<p id=\"Arti00011998\">For this, write the following rules in kb5.pl and execute the predicate as shown below.<\/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_p584-2.png\" alt=\"images\"\/><\/figure>\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_p584-3.png\" alt=\"images\"\/><\/figure>\n","protected":false},"excerpt":{"rendered":"<p>Program 1:&nbsp;Check whether the list has odd number of elements or the even number of elements. To implement this functionality, we will define predicates list_even_len(L) and list_odd_len(L) while considering the following points. Write the following rules in kb5.pl and execute the commands as shown below. Credit: Copyright (C) 1999-2021 Daniel Diaz Program 2:&nbsp;Find the maximum [&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-3816","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\/3816","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=3816"}],"version-history":[{"count":1,"href":"https:\/\/workhouse.sweetdishy.com\/index.php\/wp-json\/wp\/v2\/posts\/3816\/revisions"}],"predecessor-version":[{"id":3817,"href":"https:\/\/workhouse.sweetdishy.com\/index.php\/wp-json\/wp\/v2\/posts\/3816\/revisions\/3817"}],"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=3816"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/workhouse.sweetdishy.com\/index.php\/wp-json\/wp\/v2\/categories?post=3816"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/workhouse.sweetdishy.com\/index.php\/wp-json\/wp\/v2\/tags?post=3816"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}