SlideServe
Menu
Préférences
© SlideServe · v2026

CSE 452: Programming Languages

gotzon avatar
14 diapositives · 272 Views · Sep 09, 2014

Explore examples of logical programming in Prolog including deleting an element from a list, combining lists, finding sublists, and implementing quick sort using functors and arguments to sort and manipulate data efficiently.

CSE 452: Programming Languages - slide 1
01 / 14CSE 452: Programming Languages
1 / 17

Télécharger la présentation

CSE 452: Programming Languages
Une image / un lien ci-dessous est fourni (tel quel) pour télécharger la présentation

Politique de téléchargement : le contenu du site vous est fourni TEL QUEL pour votre information et votre usage personnel ; il ne peut être vendu, concédé sous licence ni partagé sur d’autres sites sans l’accord de son auteur.Le contenu vous est fourni TEL QUEL, pour votre information et votre usage personnel uniquement. Téléchargez la présentation en cliquant sur ce lien. Lors du téléchargement, si vous ne parvenez pas à télécharger une présentation, l’éditeur a peut-être supprimé le fichier de son serveur.Pendant le téléchargement, si vous ne parvenez pas à obtenir une présentation, le fichier a peut-être été supprimé par l’éditeur.

F I N

Plus de contenu associé

Transcription de la présentation
  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).