{"id":796,"date":"2010-10-13T09:53:32","date_gmt":"2010-10-13T13:53:32","guid":{"rendered":"http:\/\/www.elblender.com\/wordpress\/?p=796"},"modified":"2010-10-14T08:04:13","modified_gmt":"2010-10-14T12:04:13","slug":"prolog-using-examples-part-4-continuation","status":"publish","type":"post","link":"http:\/\/blog.acarlstein.com\/?p=796","title":{"rendered":"Prolog using Examples &#8211; Part 5"},"content":{"rendered":"<p>The follow is a continuation of the previous posting &#8220;Prolog using Examples &#8211; Part 4&#8221;<\/p>\n<p><span style=\"text-decoration: underline;\">Example 1:<\/span> Check the prefix of a list.<\/p>\n<pre><span style=\"color: #0000ff;\">\/* Base case *\/\r\n\/* When the list of prefix checked is empty means that we stop checking *\/\r\nprefix([],_).\r\n\r\n\/* Recursive case *\/\r\n\/* Both head of the list must be the same *\/\r\nprefix([Head|Tail_A], [Head|Tail_B]):-\r\n\u00a0\u00a0\u00a0 prefix(Tail_A, Tail_B).\r\n\r\n?- prefix([1,b], [1,b,c,4]).\r\ntrue.\r\n\r\n?- prefix([1,b], [1,d,c,4]).\r\nfalse.\r\n\r\n<\/span>\r\n<span style=\"color: #0000ff;\">\/* prefix will search for each prefix everytime OR ';' is used *\/\r\n?- prefix(Lst, [1,2,3,4]).\r\nLst = [] ;\r\nLst = [1] ;\r\nLst = [1, 2] ;\r\nLst = [1, 2, 3] ;\r\nLst = [1, 2, 3, 4] ;\r\nfalse.\r\n\r\n\/* Generate a list where list [1,2,3,4] is the prefix and the tail is a dynamic variable *\/\r\n?- prefix([1,2,3,4],Lst).\r\nLst = [1, 2, 3, 4|_G3713].\u00a0<\/span><span style=\"color: #0000ff;\">\r\n<\/span><\/pre>\n<p><span style=\"text-decoration: underline;\">Example 2:<\/span> Check the suffix of a list.<\/p>\n<pre><span style=\"color: #0000ff;\">\/* Base case *\/\r\n\/* When both list are the same, we found the suffix *\/\r\nsuffix(Lst_tail, Lst_tail).\r\n\r\n\/* Recursive case *\/\r\n\/* We want to check always the tail of the list, so the base case can check if they are the same *\/\r\nsuffix([_|Tail], Lst) :-\r\n\u00a0\u00a0\u00a0 suffix(Tail, Lst).\r\n\r\n?- suffix([a,2,c,4],[c,4]).\r\ntrue.\r\n\r\n?- suffix([a,2,c,d],[c,4]).\r\nfalse.\r\n\r\n\/* Obtain the next tail everytime OR ';' is used *\/\r\n?- suffix([1,2,3,4], Lst).\r\nLst = [1, 2, 3, 4] ;\r\nLst = [2, 3, 4] ;\r\nLst = [3, 4] ;\r\nLst = [4] ;\r\nLst = [].\r\n\r\n\/* Be careful, in this kind of cases prolog keep generating dynamic variables in from of the list *\/\r\n?- suffix(Lst, [1,2,3,4]).\r\nLst = [1, 2, 3, 4] ;\r\nLst = [_G3704, 1, 2, 3, 4] ;\r\nLst = [_G3704, _G3707, 1, 2, 3, 4] ;\r\nLst = [_G3704, _G3707, _G3710, 1, 2, 3, 4] ;\r\nLst = [_G3704, _G3707, _G3710, _G3713, 1, 2, 3, 4] ;\r\nLst = [_G3704, _G3707, _G3710, _G3713, _G3716, 1, 2, 3, 4] ;\r\nLst = [_G3704, _G3707, _G3710, _G3713, _G3716, _G3719, 1, 2, 3|...] ;\r\n... \/* Keep generating dynamic variables *\/\r\n\r\n<\/span><span style=\"color: #0000ff;\">\u00a0<\/span><\/pre>\n<p><span style=\"text-decoration: underline;\">Example 3:<\/span> In this example, we are introducing <em>append<\/em>. <em>append<\/em> is the unification of two lists, List_A, List_B, into one list that we are going to call list AB, List_AB.<\/p>\n<p style=\"text-align: center;\"><a href=\"http:\/\/www.elblender.com\/wordpress\/wp-content\/uploads\/2010\/10\/append_case_1.png\"><img loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-842 aligncenter\" title=\"append_case_1\" src=\"http:\/\/www.elblender.com\/wordpress\/wp-content\/uploads\/2010\/10\/append_case_1.png\" alt=\"\" width=\"399\" height=\"120\" srcset=\"http:\/\/blog.acarlstein.com\/wp-content\/uploads\/2010\/10\/append_case_1.png 399w, http:\/\/blog.acarlstein.com\/wp-content\/uploads\/2010\/10\/append_case_1-300x90.png 300w\" sizes=\"auto, (max-width: 399px) 100vw, 399px\" \/><\/a><\/p>\n<p style=\"text-align: left;\">The code of <em>append<\/em> is the follow:<\/p>\n<pre style=\"text-align: left;\"><span style=\"color: #0000ff;\">\/* Base case *\/\r\n\/* Will stop recursion when List_A is empty indicating that all the elements are in List_AB *\/\r\nappend([], Lst, Lst).\r\n\r\n\/* Recursive case *\/\r\nappend([Head|Tail_A], List_B, [Head|Tail_AB]):-\r\n\u00a0\u00a0\u00a0 append(Tail_A, List_B, Tail_AB).<\/span><\/pre>\n<p>First <em>append<\/em> will move 1, 2,\u00a0 and 3 atoms from List_A to List_AB using recursion:<\/p>\n<pre><span style=\"color: #0000ff;\">\/* Recursive case *\/\r\nappend([Head|Tail_A], List_B, [Head|Tail_AB]):-\r\n\u00a0\u00a0\u00a0 append(Tail_A, List_B, Tail_AB).<\/span><\/pre>\n<p><em><span style=\"color: #ff6600;\">append([1,2,3], [4,5,6], []) \u2192 append([2,3], [4,5,6], [1])\u00a0 \u2192 append([3], [4,5,6], [1,2])\u00a0 \u2192 append([], [4,5,6], [1,2,3]) <\/span><\/em><\/p>\n<p>When List_A is empty, then it will append List_B to List_AB as a tail of List_AB using the base case:<\/p>\n<pre><span style=\"color: #0000ff;\">\/* Base case *\/\r\n\/* Will stop recursion when List_A is empty indicating that all the elements are in List_AB *\/\r\nappend([], Lst, Lst).\r\n\r\n<\/span><\/pre>\n<p><em><br \/>\n<span style=\"color: #ff6600;\">append([], [4,5,6], [1,2,3]) \u2192 append([], [], [1,2,3,4,5,6])\u00a0 \u2192 List_AB = [1,2,3,4,5,6]<\/span><\/em><\/p>\n<p style=\"text-align: left;\"><span style=\"text-decoration: underline;\">Example 4:<\/span> Even do <em>append<\/em> seems to create a list by appending List_A with List_B, <em>append<\/em> can behave in a different ways depending of how it is used:<\/p>\n<p style=\"text-align: left;\"><span style=\"text-decoration: underline;\">Let see the code for <em>append<\/em>:<\/span><\/p>\n<pre><span style=\"color: #0000ff;\">append([], Lst, Lst). \/*\u00a0\u2190 Base case *\/\r\nappend([Head|Tail_A], List_B, [Head|Tail_AB]):-\u00a0 \/* \u2190 Recursive case *\/\r\n\u00a0\u00a0\u00a0 append(Tail_A, List_B, Tail_AB).\r\n\r\n<\/span><span style=\"color: #0000ff;\">\u00a0<\/span><\/pre>\n<p>Lets say we do\u00a0 <span style=\"color: #0000ff;\">append(List_A, [5,6], [1,2,3,4,5,6])<\/span>. What would be the result?<\/p>\n<p style=\"text-align: center;\">\n<p><a href=\"http:\/\/www.elblender.com\/wordpress\/wp-content\/uploads\/2010\/10\/append_case_2.png\"><img loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-861 aligncenter\" title=\"append_case_2\" src=\"http:\/\/www.elblender.com\/wordpress\/wp-content\/uploads\/2010\/10\/append_case_2.png\" alt=\"\" width=\"399\" height=\"160\" srcset=\"http:\/\/blog.acarlstein.com\/wp-content\/uploads\/2010\/10\/append_case_2.png 399w, http:\/\/blog.acarlstein.com\/wp-content\/uploads\/2010\/10\/append_case_2-300x120.png 300w\" sizes=\"auto, (max-width: 399px) 100vw, 399px\" \/><\/a><\/p>\n<p style=\"text-align: left;\">As you can see List_AB have 1,2,3,4,5, and 6 while List_B have 5 and 6, Prolog will unify List_A with 1,2,3, and 4.<\/p>\n<pre><span style=\"color: #0000ff;\">?- append(List_A, [5,6], [1,2,3,4,5,6]).\r\nList_A = [1, 2, 3, 4] .<\/span><\/pre>\n<p style=\"text-align: left;\">\n<p style=\"text-align: left;\">\n<p style=\"text-align: left;\">\n<p style=\"text-align: left;\">\n<p style=\"text-align: left;\">\n<p style=\"text-align: left;\">\n<p style=\"text-align: left;\">\n<p style=\"text-align: left;\">\n\n<script>\nvar zbPregResult = '0';\n<\/script>\n","protected":false},"excerpt":{"rendered":"<p>The follow is a continuation of the previous posting &#8220;Prolog using Examples &#8211; Part 4&#8221; Example 1: Check the prefix of a list. \/* Base case *\/ \/* When the list of prefix checked is empty means that we stop checking *\/ prefix([],_). \/* Recursive case *\/ \/* Both head of the list must be [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[19,146],"tags":[180,181,184,1312,183],"class_list":["post-796","post","type-post","status-publish","format-standard","hentry","category-programming","category-prolog","tag-list","tag-lists","tag-prefix","tag-prolog","tag-suffix"],"_links":{"self":[{"href":"http:\/\/blog.acarlstein.com\/index.php?rest_route=\/wp\/v2\/posts\/796","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/blog.acarlstein.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/blog.acarlstein.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/blog.acarlstein.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/blog.acarlstein.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=796"}],"version-history":[{"count":27,"href":"http:\/\/blog.acarlstein.com\/index.php?rest_route=\/wp\/v2\/posts\/796\/revisions"}],"predecessor-version":[{"id":863,"href":"http:\/\/blog.acarlstein.com\/index.php?rest_route=\/wp\/v2\/posts\/796\/revisions\/863"}],"wp:attachment":[{"href":"http:\/\/blog.acarlstein.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=796"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/blog.acarlstein.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=796"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/blog.acarlstein.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=796"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}