CSE 452: Programming Languages

1 / 14

# CSE 452: Programming Languages - PowerPoint PPT Presentation

CSE 452: Programming Languages Logical Programming Languages Part 3 Acknowledgements P. Tan http://www.csupomona.edu/~jrfisher/www/prolog_tutorial/contents.html http://cs.wwc.edu/~cs_dept/KU/PR/Prolog.html http://pauillac.inria.fr/~diaz/gnu-prolog/ Prolog Example

I am the owner, or an agent authorized to act on behalf of the owner, of the copyrighted work described.

## PowerPoint Slideshow about 'CSE 452: Programming Languages' - ivanbritt

Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author.While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server.

- - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - -
Presentation Transcript

### CSE 452: Programming Languages

Logical Programming Languages

Part 3

Acknowledgements
• P. Tan
• http://www.csupomona.edu/~jrfisher/www/prolog_tutorial/contents.html
• http://cs.wwc.edu/~cs_dept/KU/PR/Prolog.html
• http://pauillac.inria.fr/~diaz/gnu-prolog/
Prolog Example
• Delete an element from a list
• deleteElem /3 ( /3 means there are 3 arguments)
• Example:
• deleteElem(4, [2,3,4,5], X)
• X = [2,3,5].
• deleteElem(6, [2,3,4,5],X)
• X = [2,3,4,5].
Delete an Element from a List
• Functor: deleteElem
• Arguments
• Input:
• An element
• List of elements
• Output:
• Result list
Delete an Element from a List
• Base step:
• deleteElem(_, [], []). %% underscore means any symbol
• deleteElem(P,[P|Tail],Tail).
• Recursive step:
• deleteElem(P,[H|Tail],[H|Res]) :- P \= H, deleteElem(P,Tail,Res).
Combining Lists
• Write a predicate combine/3 that takes three lists as arguments and combines the elements of the first two lists into the third as follows:

?- combine([a,b,c],[1,2,3],X).

X = [a,1,b,2,c,3]

?- combine([foo,bar,yip,yup],[glub,glab,glib,glob],Result).

Result = [foo,glub,bar,glab,yip,glib,yup,glob]

Combining Lists

combine([],List,List).

combine(List,[],List).

combine([H|T],[H2|T2],[H,H2|Res]) :- combine(T,T2,Res).

Sublist
• Sublist/2 is true if the first argument is a sublist of the second argument. For example:

sublist([c,d,e],[a,b,c,d,e,f]) is true

sublist([c,,e], [a,b,c,d,e,f]) is false

sublist(S,L):-

append(L1,L2,L),

append(S,L3,L2).

Sublist
• use it in different ways:

sublist([c,d,e],[a,b,c,d,e,f]).

sublist([c,,e], [a,b,c,d,e,f]).

sublist(S,[a,b,c]).

• uses backtracking to find all possible sublists.
Quick Sort

p

Partition

< p

p

>= p

Sort

Sort

Quick Sort
• What are the functors?
• quickSort
• partition
• How many arguments?
• quickSort
• Input: Unsorted list
• Output: Sorted list
• partition
• Input:
• pivot
• List of numbers (besides the pivot) to be partitioned
• Output:
• Left list (list of numbers less than pivot)
• Right list (list of numbers greater than or equal to pivot)
Quick Sort
• Base step:
• quickSort([], []).
• Recursive step:
• quickSort([P | T], Result) :- partition(T, P, L, B),

quickSort(L, SortLow), quickSort(B, SortBig),

append(SortLow, [P | SortBig], Result).

Partition
• Base step:
• partition([ ], P, [ ], [ ]).
• Recursive step:
• partition([H|T],P,[H|L],B) :-

H < P, partition(T, P, L, B).

• partition([H|T],P,L,[H|B]) :-

H >= P, partition(T, P, L, B).

Quick Sort (Summary)

quickSort([ ], [ ]).

quickSort([P | T], Y) :- partition(T, P, L, B),

quickSort(L, SortLow), quickSort(B, SortBig),

append(SortLow, [P | SortBig], Y).

partition([ ], P, [ ], [ ]).

partition([H | T], P, [H | L], B) :- H < P, partition(T, P, L, B).

partition([H | T], P, L, [H | B]) :- H >= P, partition(T, P, L, B).