50 likes | 175 Views
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 ).
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