1 / 5

Prolog Fruit Relation Example: Querying Possessions and Basic Arithmetic

This Prolog script demonstrates the usage of facts and queries related to fruit ownership. The program includes facts like "jack has apples," "ann has plums," and "dan has money." It allows for querying who owns what fruit, identifying individuals with specific fruits, and checking for possession of non-fruit items. Additionally, it showcases basic arithmetic operations like addition, subtraction, multiplication, square roots, and absolute values. It serves as a practical illustration of Prolog's capabilities in handling relationships and arithmetic calculations.

xanti
Télécharger la présentation

Prolog Fruit Relation Example: Querying Possessions and Basic Arithmetic

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. listing(fruit).: Lists all the objects within this relation halt. Exit from Prolog write(X). /* Write to console */ nl/* New Line */ ! /* CUT Backtrack */ Built-in Functions

  2. Example I: Simple Program has(jack,apples). has(ann,plums). has(dan,money). fruit(apples). fruit(plums). Queries: • has(jack,_). /* Don’t care about second operand  */ • has(X,apples),has(Y,plums). /* who has apples and who has plums? */ • has(dan,X),fruit(X). /* has Dan fruits? */ • has(X,Y),not fruit(Y). /* does someone have something else? */

  3. Basic Arithmetic ?- X is 5+4, Y is 5-4. X = 9, Y = 1  ?- X is 5*4, Y is 2^3. X = 20 , Y = 8 ?- X is sqrt(3),Y is 3^0.5. X = 1.73205080756888, Y = 1.73205080756888  ?- X is 8 mod 3. X = 2

  4. Example II: Arithmetic abs(X,X):- X >= 0, !. abs(X,Y):- Y is -X.  ?- abs(0,R). R = 0 ?- abs(-9,R). R = 9

  5. Example III: Lists

More Related