0% found this document useful (0 votes)
62 views4 pages

IIMS College: Putalisadak, Kathmandu, Nepal

The document contains a programming assignment for a Prolog course. It asks the student to write Prolog code for 7 queries: 1) doubled list, 2) palindrome, 3) second element, 4) swap first two elements, 5) last element, 6) remove first and last elements, 7) swap first and last elements. For each query, it provides an example and description of what the code should do. The student is asked to provide the Prolog code and output for each query.

Uploaded by

Oum Singh
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as ODT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
62 views4 pages

IIMS College: Putalisadak, Kathmandu, Nepal

The document contains a programming assignment for a Prolog course. It asks the student to write Prolog code for 7 queries: 1) doubled list, 2) palindrome, 3) second element, 4) swap first two elements, 5) last element, 6) remove first and last elements, 7) swap first and last elements. For each query, it provides an example and description of what the code should do. The student is asked to provide the Prolog code and output for each query.

Uploaded by

Oum Singh
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as ODT, PDF, TXT or read online on Scribd
You are on page 1/ 4

IIMS College

Putalisadak, Kathmandu, Nepal

Name of Course Instructor: _Purnima Mulmi___________________________________________________

Course Code: __CC303n_________         Course Name: __Programming Models_____________________

Program Name: B.Sc(Hons).Computing             Semester: Seven______                    Batch: _Sept 2018

Coursework I / II / III: _____ _____ Assignment Type (Individual/Group): Individual________________

Assignment Title: Give the suitable Prolog query for the following questions__________________________

Max. Marks: ______                 Date of Allotment: 05/12/2020___    Date of Submission: 05/13/2020

(Write the individual/group members details below):


Name of the Student UCSI ID number Contact Number Email Id
Om Narayan Singh 1001852431 9860026839 [email protected]

Evaluation: ________________________ obtained out of ____________________________

Evaluator’s Comment: ____________________________________________________________

________________________________________________________________________________

____________________________________________________________________________

-------------------------------------------
Evaluator’s Signature & Date
1. Let's call a list doubled if it is made of two consecutive blocks of elements that are exactly
the same. For example, [a,b,c,a,b,c] is doubled (it's made up of [a,b,c] followed by [a,b,c])
and so is [foo,gubble,foo,gubble]. On the other hand, [foo,gubble,foo] is not doubled. Write
a predicate doubled(List) which succeeds when List is a doubled list.  
Ans: CODE:

doubled(List) :- append(X, X, List).

OUTPUT:

2. A palindrome is a word or phrase that spells the same forwards and backwards. For example,
`rotator', `eve', and `nurses run' are all palindromes. Write a predicate palindrome(List),
which checks whether List is a palindrome. For example, to the queries
?- palindrome([r,o,t,a,t,o,r]).
and
?- palindrome([n,u,r,s,e,s,r,u,n]).
Prolog should respond `yes', but to the query
?- palindrome([n,o,t,h,i,s]).
Prolog should respond `no'.  
Ans: CODE:

palindrome(List) :- reverse(List,List).

OUTPUT:

3. Write a predicate second(X,List) which checks whether X is the second element of List.
Ans: CODE:

second(X,[_,X|_]).
OUTPUT:

4. Write a predicate swap12(List1,List2) which checks whether List1 is identical to List2,


except that the first two elements are exchanged.
Ans: CODE:

swap12([X,Y|T],[Y,X|T]).

OUTPUT:

5. Write a predicate final(X,List) which checks whether X is the last element of List.  
Ans: CODE:

final(X,List) :- reverse(List,[X|_]).

OUTPUT:

6. Write a predicate toptail(InList,Outlist) which says `no' if inlist is a list containing fewer
than 2 elements, and which deletes the first and the last elements of Inlist and returns the
result as Outlist, when Inlist is a list containing at least 2 elements. For example:
toptail([a],T).
no
toptail([a,b],T).
T=[]
toptail([a,b,c],T).
T=[b]
Hint: here's where append comes in useful. 
Ans: CODE:

toptail([_|Xs],Outlist) :- append(Outlist,[_],Xs).

OUTPUT:

 
7. Write a predicate swapfl(List1,List2) which checks whether List1 is identical to List2,
except that the first and last elements are exchanged. Hint: here's where append comes in
useful again.  
Ans: CODE:

swapfl([X|Xs],List2) :-

append(T,[H],Xs),

append([H|T],[X],List2).

OUTPUT:
 

You might also like