Computer Science, asked by reshmababy9222, 10 months ago

List the components of prolog.

Answers

Answered by Ankit2008
0

Answer:

Any language needs a way to handle collections of objects and prolog is no exception. A list in prolog can look like this:

[a, b, c, d, e]

The brackets are the beginning and the end of the list, and the commas separate the various elements. Here all the elements are atoms, but a list can contain all sorts of elements, and different types of element can occur in the same list. The following are examples of valid lists:

[a, A, B, C, D]

[p(A,V), p(a, V)]

[[a1,a2,a3],[b1,b2,b3],[c1,c2,c3]]

[[A,B], [B,C], quu([a,b,c],[a,b,d,e,f],_), c, m, D]

Most prolog code will not define a list explicitly, like these examples, but rather handle lists of any length, with many possible elements. To handle lists without knowing what's inside them or how long they are, you use the bar notation:

[Head|Tail]

In this notation the variable Head represents the leftmost element of the list and the variable Tail represents the rest of the list represented as another list. A head can be anything, from a predicate to another list, but the tail is always another list.

Some default library rules are provided, e.g., length, reverse, append (also, these can be defined easily, as shown at the bottom of this page). To see how these work, try the following codes:

?- length([1, 3, 6], What).

Prolog will answer 3.

?- append([a], b, Z).

?- append([a], [b], Z).

Observe the difference here.

?- append(X, [1, 2], [1, 1, 2]).

?- reverse([1,2], What).

The following program shows how to use this.

listsplit([H|T], H, T).

Here, [H|T] is a list, H is the head, and T is the tail. For the query

?- listsplit([a,b,c,d,e], a, [b,c,d,e]).

Prolog will answer yes. For the query

?- listsplit([a,b,c,d,e], A, B).

Prolog will answer:

A = a

B = [b, c, d, e] ;

The program can even be used to 'create' a list:

?- listsplit(List, a, [b,c,d,e]).

List = [a, b, c, d, e] ;

here are some examples of lists, and what heads and tails they result in:

List Head Tail

[a,b,c,d,e]

a

[b,c,d,e]

[a]

a

[] (an empty list)

[[a,b,c],a,b,[a,b]]

[a,b,c]

[a,b,[a,b]]

Note that the empty list [ ] cannot be split up and therefore will not unify with [H|T].

Splitting up lists can be done with more than two heads:

[H1, H2, H3|Tail]

This will split a list up into the first three elements, and the rest of the list. Note, however that this will fail if the list has fewer than three elements.

Now consider the following program:

last([Elem], Elem).

last([_|Tail], Elem) :- last(Tail, Elem).

This relation defines the last element of a list. It can be used as a program to find a list's last element:

?- last([a,b,c,d,e],X).

X = e

First, there is the stop predicate, that says that the last element of a list with one element, is that element. The second line says that the last element (Elem) of a list [_|Tail] is the last element of its tail (Tail). Since we don't care about the head, we use the anonymous variable, _, for it.

Similar questions