solve the lab assessment by visual prolog please
Lab 04 - List Processing in Prolog Objective This lab explains list processing in prolog and the different operations performed on the list. Current Lab Learning Outcomes (LLO) Students should get familiar with: How to use list in prolog How to insert an element in the list List unification Counting the elements of a list Outline List processing Membership in List List of integers List of mixed types (compound objects) List unification Inserting elements into list Counting the elements of a list Examples and Exercise Lab Requirements PROLOG Lab Description List: List is a sequence of any number of items Example: (mech, clec, civil, aero, ese, john, [1,2]..[X]] List consists of two parts i.e L - [Head |Tail] Head - First item of the list Tail - Remaining part of the list (tail itself is a list) Empty list is also a list (1) Lists are handled in Prolog as trees. Listita,b,c [a,b,c) - [a[b.c] ]-[a, b][c] - [a,b,c011 Membership: member (XL): X is an object that occurs in L. X is a member of list Lif either X is the head of Lor X is a member of the tail of L. membert X. [XI Taill) membert X. (Head Taill): membert X. Tail) Example: : 7 member a, (a,b,c). Yes 7 member (5.c). [a. [bc]]). Yes, ? memberb. [1, [b.c]]). No. List Processing List processing allows handling objects that contain an arbitrary number of elements. A list is an object that contains an arbitrary number of other objects. A list that contains the numbers 1, 2 and 3 is written as [1,2,3]. Each item contained in a list is known as an element. To declare the domain of a list of integers: DOMAINS list integer %means list of integers The elements of a list must be of a single domain. To define a list of mixed types use compound objects as follows: DOMAINS clementlist - clement element -- ie (integer); re (real): se (string) A list consists of two parts: Head: the first dement of the list Tail: a list of the remaining elements Example: if 1={a,b,c) the head is a and the tail is b,c). Procedure: If you remove the first element from the tail of the list enough times, you get down to the empty list ( 1). The empty list can not be broken into head and tail. A list has a tree structure like compound objects. Example: the tree structure of (a, b,c,d) is drawn as shown in the figure. Note the one element list (al is not as the element a since (al is really the compound data structure shown here 11 List Processing: Prolog allows trcating the Head and Tail explicitly. You can separate the list head and tail using the vertical bar (1). Example: : . [a b [ el [a b COD [c] . List unification: Following table gives examples of list unification, List unification: the following table gives examples of list unification: list 1 list2 Variable binding (X, Y, Z] [ book, ball, pen] X=book, Y=ball, Zepen [7] IXIY] X=7.Y=0) [1, 2, 3, 4] IX, YIZ X=1.Y=2.Z=[3.4) [1,2] [3IX] fail Using Lists: Using lists - Since a list is really a recursive DOMAINS compound data structure, lista integer you need recursive algorithms to process it. PREDICATES Such an algorithm is usually write_a_list( list) Do nothing composed of two clauses: but report CLAUSES One to deal with empty list. success The other deals with ordinary write_a_list(t1). list (having head and tail). TH1).write_a_list(MIT) Example: the shown program writes the elements write(H), Write the head of an integer list nt, write_a_list(T). Write the GOAL write_2_list(1, 2, 3]). Example: Inserting elements inte tist. DOMAINS list integer PREDICATES write a list(list) CLAUSES write_a_list)). write a list [HT)):- write(H). nl.nl represents New Line write_a_list(T) GOAL write_a_list([1.2.3) % write_a_list[1,2,3,4,5) Visual Prolog Licensed to Javier Enriquez - (noname.pro Fille Edit Project Purchase Web Options Window Help PH ES 200 ORG SEE 18:1 Indent DO list Integer PREDICATES write_-_11st ist) CLOSES Buy ite_2_1 online write_2_1195]): write(lt). VISA nl representa New Line write_4_113). GOAL write_4_11311,2,31). write_e_List(1.2.3.4.51). Output: Jinactive Users\QureshilAppData\Local Templosis yes Counting the elements of a list: DOMAINS liste integer PREDICATES length_of(list, integer) CLAUSES length_of (11. o. Counting list elements Two logical rules are used to determine the length of a list 1. The length ofis 0. 2. The length of the list [X|T] is 1+the length of T. The shown Prolog program counts the number of elements of a list of integers (can be used for any type) Length of (IMITIL) length_of (T.M). LEM+1. GOAL length_of( 1, 2, 3...). Example: Counting the elements of a list: DOMAINS list-integer PREDICATES length_of_list(list, integer) CLAUSES length_of_list([],0). length_of_list[HT), L- length_of_list(T.M), L-M+. GOAL length of_list(1,2,3), L.). % length of_list([1.2.3.4.5). L). Visual Prolog Licensed to Javier Enriquez - (noname pro File Edit Project Purchase Web Options Window Help 16:33 Insert Indent Hlodie DOINS liat integer PREDICATES Length_c_31st, Integer) Buy CLAUSES online Length_of_11a (1.0). VISA length_at_1135 (t=15], 1);- length_of_1130 (1.4), 14+. GOAL length_0_1131,2,31,1). length_of_list([1,2,3,4,5).L). Output: active CaldersQuresh AppData Local Tempigow 5000 exe) 1 Solution Lab Assessment 1. Write a prolog program to insert Real numbers into a list. 2. Write a prolog program to insert Strings into a list. Extra (supplementary) Materials Prolog Programming for Artificial Intelligence by Ivan Bratko, 4th edition - Chapter 3