cse 452 programming languages l.
Download
Skip this Video
Loading SlideShow in 5 Seconds..
CSE 452: Programming Languages PowerPoint Presentation
Download Presentation
CSE 452: Programming Languages

Loading in 2 Seconds...

play fullscreen
1 / 14

CSE 452: Programming Languages - PowerPoint PPT Presentation


  • 346 Views
  • Uploaded on

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

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

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


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

CSE 452: Programming Languages

Logical Programming Languages

Part 3

acknowledgements
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
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
Delete an Element from a List
  • Functor: deleteElem
  • Arguments
    • Input:
      • An element
      • List of elements
    • Output:
      • Result list
delete an element from a list5
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
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 lists7
Combining Lists

combine([],List,List).

combine(List,[],List).

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

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

sublist9
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
Quick Sort

p

Partition

< p

p

>= p

Sort

Sort

quick sort11
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 sort12
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
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
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).