itec 380 n.
Download
Skip this Video
Loading SlideShow in 5 Seconds..
ITEC 380 PowerPoint Presentation
Download Presentation
ITEC 380

Loading in 2 Seconds...

play fullscreen
1 / 16

ITEC 380 - PowerPoint PPT Presentation


  • 178 Views
  • Uploaded on

ITEC 380. Organization of programming languages Lecture 9 – Prolog. Review. Prolog What are its 2 major components ? Where are facts stored? What is their relationship with the interpreter? What is the difference between upper and lower? User I/O

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 'ITEC 380' - gaenor


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
itec 380

ITEC 380

Organization of programming languages

Lecture 9 – Prolog

review
Review
  • Prolog
    • What are its 2 major components?
  • Where are facts stored? What is their relationship with the interpreter?
  • What is the difference between upper and lower?
  • User I/O
  • Email me a 3rd generation OO language by the end of the week or I’ll pick one!
objectives
Objectives
  • Homework 2
  • Prolog
    • Recursion
    • Lists
notes
Notes
  • When you see …/2 in prolog that means a rule that takes 2 parameters
  • Is variable, and lets you know what the rule expects if there is an issue
  • Can have multiple rules, facts with same name
    • Friend(X) :- Person1(X).
    • Friend(X) :- Person2(X).
exercise
Exercise
  • Time for a bit of magic
  • There is a problem with this example, can you spot it?

house_elf(dobby).    witch(hermione).    witch(’McGonagall’).    witch(rita_skeeter).    magic(X):-  house_elf(X).    magic(X):-  wizard(X).    magic(X):-  witch(X).

exercise1
Exercise
  • What is the difference between
    • read(r), X is r * r, write(X).
    • read(R), X is R * R, write(X).
  • Simple calculations
    • The area of a circle is 2*pi*r
    • Pythagoras (given a and b [sides], what is c [hypotenuse]
recursion
Recursion
  • What is recursion?
    • What are the benefits?
    • What are the downsides?
  • Prolog syntax
    • rule(parameters) :- rule(parameters), baseCase(parameters).
  • Remember
    • Always use the base case
example
Example
  • Descendants
    • Anne has a daughter named Bridget
    • Bridget has a daughter named Caroline
    • Caroline has a daughter named Donna
    • Donna has a daughter named Emily
  • How would you represent this information?
  • How would you specify the ability to query if a person is a descendant of another?
  • What are some uses for this type of query?
question
Question
  • What is the difference between

child(anne,bridget).

child(bridget,caroline).

child(caroline,donna).

child(donna,emily).

descend(X,Y) :- descend(Z,Y), child(X,Z).

descend(X,Y) :- child(X,Y).

and

descend(X,Y) :- child(X,Y).

descend(X,Y) :- descend(Z,Y), child(X,Z).

caution
Caution
  • Run away stacks
    • Always recurses in the order of rules
    • Must make sure you stop!
    • What about bad input?
  • Very easy to cause an infinite loop through bad queries
  • One of the most common problems, limiting factors of prolog
lists
Lists
  • In prolog you use [ ] to denote a list
  • List items are separated by ,’s
  • Lists can contain lists
  • Can query certain parts of the list like Lisp
  • For example
    • [Head|Tail] = [apple, pear, peach].
    • What do you think Head contains? What about Tail?
  • Exercise
    • Write a function to give you the third item in a list
contents
Contents
  • Empty list is [ ], can be checked with Var = [ ]
  • What happens if you combine not(Var = [ ]) in a rule?
  • Can do more than [ X | Y ], for example [ X,Y,Z | W ]
  • Can append data with append(list1,list2, Var)
    • Allows you to build up a list
  • Can figure out length with Length(list,X).
  • Member(X,[list]) returns with value or true/false depending on variable or constant
examples
Examples
  • PrintList(L). %Print the contents of the list
  • reversePrint(L). %Print the contents of the list in reverse
  • revList(L). %Reverse a list
  • Palindrome checker
    • Is a word spelled the same forwards / backwards
exercise2
Exercise
  • How would you create a stack that allows users to add values to it?
  • How would you use this stack with a command that will add the 2 top numbers on the stack (removing them and adding the result to the top of the stack)?
  • How do we tie this in so it allows commands to be entered multiple times?
methods of reversing
Methods of reversing
  • Accumulator versus appending
  • How do we tell which one is better?

naiverev([],[]). naiverev([H|T],R):-  naiverev(T,RevT),  append(RevT,[H],R).

Versus

accRev([H|T],A,R):-  accRev(T,[H|A],R). accRev([],A,A).

rev(L,R):-  accRev(L,[],R).

next week
Next week
  • More in-depth with prolog
    • Graphics