You are on page 1of 3

Ankit negi

2k18CSUN01011
CSE-4A
(PT2 - AI)
Q1. Domains
List=symbol*

Predicates
Con(list,list,list)
Clauses

Con([],L1,L1).
Con([X!tail],L2,[X!tail1]):-con(tail,L2,tail)

OUT PUT
=======
Goal:con([a,b,c],[d,e],concat list)
Concatlist=(“a”,”b”,”c”,”d”,”e”]
1 solution

Q2. domains
namelist=symbol*
predicates
last(nmaelist,symbol)
clauses
last([Head],X):-X=Head.
last([_|Tail],X):-last(Tail,X).
OUTPUT
n1([1,2,3],[5,6,7],L)
L=[1,2,3,4,5,6,7]
n19[],[1,2,3,4],A)
A=[1,2,3,4]
last([abcd],X0
X=abcd
last([a,s,d,f,g],A)
A=g

Q3. domains

list=symbol*

predicates

len(list)
findlen(list,integer)

clauses

len(X):-
findlen(X,Count),
write(\"\\nLength Of List : \"),
write(Count).

findlen([],X):-
X=0.

findlen([X|Tail],Count):-
findlen(Tail,Prev),
Count = Prev + 1.

OUT PUT
=======

Goal: len([a,b,c,d,e])

Length Of List : 5

Yes

VIVA
QI Define a list with an example.
A list in Prolog is a collection of terms, which is useful for grouping items together, or
for dealing with large volumes of related data, etc.
Examples. ~ [red, white, black, yellow]
Lists are enclosed by square brackets, and items are separated by commas.

Q2 Write a rule to find the grandfather of a person.


grandparent(Grandparent, Grandchild) :- parent(Grandparent, Child), parent(Child,
Grandchild).

Q3 What will be the output of a rule for combining two lists, if one of the lists is
empty?
Only the one list having some elements will be printed.

You might also like