{"id":786,"date":"2010-10-12T18:57:59","date_gmt":"2010-10-12T22:57:59","guid":{"rendered":"http:\/\/www.elblender.com\/wordpress\/?p=786"},"modified":"2010-11-12T17:55:33","modified_gmt":"2010-11-12T21:55:33","slug":"prolog-using-examples-part-4","status":"publish","type":"post","link":"http:\/\/blog.acarlstein.com\/?p=786","title":{"rendered":"Prolog using examples &#8211; Part 4"},"content":{"rendered":"<p>In this part of this practical tutorial, we are going to do some coding involving facts, rules, recursion, and lists.<\/p>\n<p><span style=\"text-decoration: underline;\">\ufeff\ufeff\ufeff\ufeff\ufeffExample 1:<\/span> Obtain the head of the list, obtain the tail of the list.<\/p>\n<pre><span style=\"color: #0000ff;\">\/* Get the head of the list *\/\r\nget_head(Head, [Head|_]).\r\n\r\n\/* Get the tail of the list *\/\r\nget_tail(Tail, [_|Tail]).\r\n\r\n?- get_head(Head_is, [a,b,c,d]).\r\nHead_is = a.\r\n\r\n?- get_tail(Tail_is, [a,b,c,d]).\r\nTail_is = [b, c, d].\r\n<\/span><\/pre>\n<p><span style=\"text-decoration: underline;\">Example 2:<\/span> Find out if an element is a member of a list.<\/p>\n<pre><span style=\"color: #0000ff;\">\/* Base case - We find the element inside the list *\/\r\n\/* The element is found if the head of the list can be unified with the element we are searching *\/\r\n\ufeffmember(Element, [Element|_]).\r\n\ufeff\r\n\/* Recursive function that call member with the tail without taking care of the head of the list *\/\r\n\/* The idea is that the base case will be called and check if the element is found *\/\r\nmember(Element, [_|Tail]) :-\r\n\u00a0 \u00a0 member(Element, Tail). \u00a0\u00a0 \ufeff\ufeff\r\n\r\n?- member(a, [a,1,3,b,c]).\r\ntrue .\r\n\r\n\/* Using OR ';', we ask Prolog to search for another element in the list *\/\r\n?- member(a, [a,1,a,b,c]).\r\ntrue ;\r\ntrue ;\r\nfalse.\r\n\r\n\/* Element is not found in the list *\/\r\n?- member(a, [c,1,3,b,c]).\r\nfalse.\r\n\r\n<\/span><\/pre>\n<p><span style=\"text-decoration: underline;\">Example 3:<\/span> Find an specific element member inside a list in a determined position.<\/p>\n<pre><span style=\"color: #0000ff;\">\/* Base case *\/\r\n\ufeff\/* The element was found in the list was found, set Position to 1 to stop recursion *\/\r\nget_member_at(1, [Head|_], Head).\r\n\r\n\/* Recursive case *\/\r\n\/* Go thought the list until position is less than 1 indicating that member was found *\/\r\nget_member_at(Position, [_|Tail], Element):-\r\n\u00a0\u00a0\u00a0 Position &gt; 1,\r\n\u00a0\u00a0\u00a0 Temp_Position is Position - 1,\r\n\u00a0\u00a0\u00a0 get_member_at(Temp_Position, Tail,\u00a0 Element).\r\n\r\n\/* Get the member at position 3 of the list *\/\r\n?- get_member_at(3, [1,3,5,7], Lst).\r\nLst = 5. \r\n\r\n\/* This fails when trying to get an element out or bounds of the list *\/\r\n?- get_member_at(5, [1,3,5,7], Lst).\r\nfalse. \r\n\r\n\/* BE CAREFUL, Sometimes prolog can behave weird base on how you use your functors eg.*\/\r\n\r\n\/* The follow is when prolog create dynamic variables because doesn't now the limit of the list *\/\r\n<\/span><span style=\"color: #0000ff;\">?- get_member_at(5, Lst, Lst).\r\nLst = [_G455, _G458, _G461, _G464, **|_G468].\r\n\r\n<\/span><span style=\"color: #0000ff;\">\/* In this case, not only produce dynamic variables to be instanciated, but also add\r\nthe list [1,a,b] as the fifth head element of the list *\/\r\n?- get_member_at(5, Lst, [1,a,b]).\r\nLst = [_G473, _G476, _G479, _G482, [1, a, b]|_G486] ;\r\n<\/span><span style=\"color: #0000ff;\">\r\n<\/span><\/pre>\n<p><span style=\"text-decoration: underline;\">Example 4:<\/span> Lets obtain the numbers of elements that belong to a simple list.<\/p>\n<pre><span style=\"color: #0000ff;\">\/* Base case - When the list is empty, unify with zero *\/\r\nnumber_of_elements([], 0).\r\n\r\n\/* Recursive case *\/\r\nnumber_of_elements([_|Tail], Number_counted) :-\r\n\u00a0\u00a0\u00a0 number_of_elements(Tail, counter),\r\n\u00a0\u00a0\u00a0 Number_counted is counter + 1.<\/span><\/pre>\n<p>In the recursive case, Prolog will call number_of_elements passing the tail of the list until the list is empty. Then, for every time number_of_elements was call it would return the counter + 1 to the previous call. At the end, we will obtain the total number of elements in the list.<\/p>\n<pre><span style=\"color: #0000ff;\">?- number_of_elements([a,1,b,2,^, &amp;], Counted).\r\nCounted = 6.\r\n\r\n\/* In this case the sublist [1,2,3] inside the list is considerate an element as a whole *\/\r\n?- number_of_elements([a,b,c,[1,2,3]], Counted).\r\nCounted = 4.\r\n\r\n?- number_of_elements([[a,b,c,1,2,3]], Counted).\r\nCounted = 1.<\/span><\/pre>\n<p>If inside the code we would had the unification symbol instead of the word &#8216;is&#8217;, we would have a different result:<\/p>\n<pre><span style=\"color: #0000ff;\">\/* Base case - When the list is empty, unify with zero *\/\r\nnumber_of_elements([], 0).\r\n\r\n\/* Recursive case *\/\r\nnumber_of_elements([_|Tail], Number_counted) :-\r\n\u00a0\u00a0\u00a0 number_of_elements(Tail, counter),<\/span><\/pre>\n<pre><span style=\"color: #0000ff;\">\u00a0\u00a0\u00a0 Number_counted is counter + 1.\r\n\r\n?- number_of_elements([a,1,b,2,^, &amp;], Counted).\r\nCounted = 0+1+1+1+1+1+1.<\/span><\/pre>\n<p><span style=\"text-decoration: underline;\">Example 5:<\/span> Lets sort a list of elements<\/p>\n<pre><span style=\"color: #0000ff;\">\/* Base case - Empty list *\/\r\nsort_list([]).\r\n\r\n\/* Base case - [_] indicate we don't case about the element, there is only one, the list is sorted *\/\r\nsort_list([_]).\r\n\r\n\/* Recursion case *\/\r\nsort_list([First_element, Second_element|Tail]):-\r\n \u00a0 \u00a0First_element =&lt; Second_element,\r\n \u00a0 \u00a0sorted([Second_element|Tail]).\r\n\r\n?- is_list_sorted([1,2,3,4]).\r\ntrue .\r\n\r\n?- is_list_sorted([1,2,3,4]).\r\ntrue ;\r\nfalse.\r\n\r\n?- is_list_sorted([1,3,3,4]).\r\ntrue.\r\n\r\n?- is_list_sorted([1,3,5,4]).\r\nfalse.\r\n\r\n<\/span><\/pre>\n<p>This topic will continue in the next posting.<\/p>\n\n<script>\nvar zbPregResult = '0';\n<\/script>\n","protected":false},"excerpt":{"rendered":"<p>In this part of this practical tutorial, we are going to do some coding involving facts, rules, recursion, and lists. \ufeff\ufeff\ufeff\ufeff\ufeffExample 1: Obtain the head of the list, obtain the tail of the list. \/* Get the head of the list *\/ get_head(Head, [Head|_]). \/* Get the tail of the list *\/ get_tail(Tail, [_|Tail]). ?- [&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,218,146],"tags":[42,175,180,181,1312,177,176],"class_list":["post-786","post","type-post","status-publish","format-standard","hentry","category-programming","category-algorithms-programming","category-prolog","tag-algorithm","tag-facts","tag-list","tag-lists","tag-prolog","tag-recursion","tag-rules"],"_links":{"self":[{"href":"http:\/\/blog.acarlstein.com\/index.php?rest_route=\/wp\/v2\/posts\/786","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=786"}],"version-history":[{"count":22,"href":"http:\/\/blog.acarlstein.com\/index.php?rest_route=\/wp\/v2\/posts\/786\/revisions"}],"predecessor-version":[{"id":791,"href":"http:\/\/blog.acarlstein.com\/index.php?rest_route=\/wp\/v2\/posts\/786\/revisions\/791"}],"wp:attachment":[{"href":"http:\/\/blog.acarlstein.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=786"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/blog.acarlstein.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=786"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/blog.acarlstein.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=786"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}