Programming Paradigms CSI2120: Jochen Lang EECS, University of Ottawa Canada
Programming Paradigms CSI2120: Jochen Lang EECS, University of Ottawa Canada
Programming Paradigms CSI2120: Jochen Lang EECS, University of Ottawa Canada
Paradigms
CSI2120
Jochen Lang
EECS, University of Ottawa
Canada
Logic Programming in Prolog
• List Representations
– dot operator
– Trees and Vine diagrams
• More List Processing
– Insertion vs. Deletion
– List processing
• Double Recursion
• Accumulators
– Examples
• Examples:
. .
pie .
X Y
fruit .
cream nil
• The notation X.Y represents the head X and the tail Y of the
list.
• The head is the first element and Y is the tail of the list.
– The notion of head and tail of a list is the basis of list
processing in Prolog.
– But the term pie.fruit is not a list just a pair
– Need brackets for a list, i.e., pie.(fruit.nil)
• Recursive sorting
• Quicksort with simply selecting the first element as the
pivot, better to randomize
sortList([],[]).
sortList([P|Q],T) :- partitionList(P,Q,G,D)
sortList(G,GG),
sortList(D,DD),
append(GG,[P|DD],T).
– Making use of the library predicate append (could use our
own definition appendList from before).
– Needs partitionList predicate (next slide).
?- mirror(L,[1,2,3,4]).
L= [4,3,2,1].
reverseList([],L,L) :- !.
reverseList([H|T],L,R) :-
reverseList(T,[H|L],R).
mirrorAcc(L,R) :- reverseList(L,[],R).
• List Representations
– dot operator
– Trees and Vine diagrams
• More List Processing
– Insertion vs. Deletion
– List processing
• Double Recursion
• Accumulators
– Examples