1 / 14

CSE 452: Programming Languages

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.

gotzon
Download Presentation

CSE 452: Programming Languages

An Image/Link below is provided (as is) to download presentation 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. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. CSE 452: Programming Languages Logical Programming Languages Part 3

  2. 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/

  3. 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].

  4. Delete an Element from a List • Functor: deleteElem • Arguments • Input: • An element • List of elements • Output: • Result list

  5. 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).

  6. 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]

  7. Combining Lists combine([],List,List). combine(List,[],List). combine([H|T],[H2|T2],[H,H2|Res]) :- combine(T,T2,Res).

  8. 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).

  9. 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.

  10. Quick Sort p Partition < p p >= p Sort Sort

  11. 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)

  12. 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).

  13. 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).

  14. 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).

More Related