50 likes | 192 Vues
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.
E N D
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
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? */
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
Example II: Arithmetic abs(X,X):- X >= 0, !. abs(X,Y):- Y is -X. ?- abs(0,R). R = 0 ?- abs(-9,R). R = 9