Docsity
Docsity

Prepare for your exams
Prepare for your exams

Study with the several resources on Docsity


Earn points to download
Earn points to download

Earn points by helping other students or get them with a premium plan


Guidelines and tips
Guidelines and tips

Notes on Introduction to Prolog Lists, Recursions, Cuts | CS 181, Study notes of Computer Science

Material Type: Notes; Class: PRINCPLS OF PROGRAMMNG LANGUAGES; Subject: Computer Science; University: University of California-Riverside; Term: Unknown 1989;

Typology: Study notes

2009/2010

Uploaded on 03/28/2010

koofers-user-rnw
koofers-user-rnw 🇺🇸

10 documents

1 / 19

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
Introduction to Prolog
Lists, Recursions, Cuts
CS181: Programming Languages
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff
pf12
pf13

Partial preview of the text

Download Notes on Introduction to Prolog Lists, Recursions, Cuts | CS 181 and more Study notes Computer Science in PDF only on Docsity!

Introduction to PrologLists, Recursions, Cuts CS181: Programming Languages

Vladimir Vacic, Christos Koufogiannakis, University of California at Riverside

Topics: ^ Lists ^ Recursion ^ Cuts

Vladimir Vacic, Christos Koufogiannakis, University of California at Riverside

Lists ^ There are two ways to represent lists

.(a,.(b,.(c,[])))[a,b,c] ^ Lists can contain other lists and variables

[ ] [the, men, [like, to, fish]][a,V1,b,[X,Y]]

Vladimir Vacic, Christos Koufogiannakis, University of California at Riverside

Lists ^ The list with^ head X

and

tail Y^ is written^ [X|Y]

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

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

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

(None) (None) [ ]

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

Tail Head List

Vladimir Vacic, Christos Koufogiannakis, University of California at Riverside

List Matching ^ Example:^ p([1, 2, 3]).p([john, likes, [football, baseball]]).?- p[X|Y].X=

Y=[2,3]

X=john

Y=[likes, [football, baseball]]

Vladimir Vacic, Christos Koufogiannakis, University of California at Riverside

Lists and Recursion ^ More examples:member(X, [X|L]).member(X, [Y|L]) :- member(X,L).last(X, [X]).last(X, [Y|L]) :- last(X, L).

Vladimir Vacic, Christos Koufogiannakis, University of California at Riverside

Recursion ^ Also you must be careful to avoid

circular

definitions

parent(X,Y) :- child(Y,X).child(A,B) :- parent(B,A).

Vladimir Vacic, Christos Koufogiannakis, University of California at Riverside

Recursions ^ Another example of a recursion (however,note that Prolog is not an efficient way to donumerical computations):factorial(0, 1).factorial(N, F) :- N>0,

N1 is N-1,factorial(N1, F1),F is N * F1.

Vladimir Vacic, Christos Koufogiannakis, University of California at Riverside

Cuts ^ Suppose that this clause has been invoked with agoal matching p(X) and the subgoals b(X) and c(X)have been satisfied. On encountering the cut:1)The cut will succeed and PROLOG will try to satisfysubgoals d(X) and e(X).2)If d(X) and e(X) succeed then p(X) succeeds.3)If d(X) and e(X) do not succeed and backtrackingreturns to the cut, then the backtracking process willimmediately terminate and p(X) fails.

Vladimir Vacic, Christos Koufogiannakis, University of California at Riverside

Cuts ^ Example:^ max(A,B,B) :- A < B.max(A,B,A).?- max(3,4,M).M = 4

M = 3

Vladimir Vacic, Christos Koufogiannakis, University of California at Riverside

Cuts ^ Avoiding useless searches:^ member(X,[X|L]) :- !.member(X,[Y|Z]) :- member([X,Z]).?- X is 5,

member(X, [5, 9, 24, 17, 5, 2]),X < 4.

Vladimir Vacic, Christos Koufogiannakis, University of California at Riverside

Cuts ^ Cuts are commonly used in conjunction withthe generate-and-test programming paradigmfind_just_one_solution(X) :-

candidate_solution(X),test_solution(X),!.

Vladimir Vacic, Christos Koufogiannakis, University of California at Riverside

References ^ Clocksin, W.F., and Mellish C.S.^ Programming in Prolog

. 4th edition. New

York: Springer-Verlag. 1994.  Aaby, A.

Prolog Tutorial

. Walla Walla

College. 1997. On line.http://cs.wwc.edu/~cs_dept/KU/PR/Prolog.html