1 / 27

Notes for CS3310 Artificial Intelligence Part 5: Prolog arithmetic and lists

Prolog arithmetic. Comparisons are written in infix form, not the usual prefix form:A == 3 (A=3 also works), A > 3, X < Y, X =< 3, Y >= 4 Arithmetic assignment is infix too, with operators , -, *, and /.For instance: X is (Y * Z) 3.This is one-way assignment: Right-side var

eustacia
Download Presentation

Notes for CS3310 Artificial Intelligence Part 5: Prolog arithmetic and lists

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. Notes for CS3310 Artificial Intelligence Part 5: Prolog arithmetic and lists Prof. Neil C. Rowe Naval Postgraduate School Version of January 2006

    2. Prolog arithmetic Comparisons are written in infix form, not the usual prefix form: A == 3 (A=3 also works), A > 3, X < Y, X =< 3, Y >= 4 Arithmetic assignment is infix too, with operators +, -, *, and /. For instance: X is (Y * Z) + 3. This is one-way assignment: Right-side variables must be bound. The left-side variable usually is unbound. Otherwise the expression checks whether the value of a variable is equal to that of an arithmetic calculation. Useful: the built-in predicate var of one argument, which succeeds if its argument is unbound.

    3. Exercises on Prolog arithmetic abs(X,AX) meaning AX is the absolute value of X. mod(X,Y,R) meaning the remainder on dividing X by Y is R. factorial(N,F) meaning F is the product of the integers from 1 through N. better_add(X,Y,S) meaning X+Y=S but where any of X, Y, or S can be a variable (using built-in var predicate that succeeds if its argument is unbound).

    4. Abs (absolute value) abs(X,X) :- X>0. abs(X,AX) :- X=<0, AX is 0-X. Behavior: ?-abs(3,A). A=3. ?-abs(B,3). B=3. ?-abs(-3,C). C=3. ?-abs(D,-3). ! Error in arithmetic expression: not a number

    5. Mod (remainder after division) and factorial Examples: ?-mod(3,5,A). A=3. ?-mod(7,5,B). B=2. ?-mod(-7,5,C). C=3 ?- factorial(5,F). F=120

    6. Better_add (more flexible addition) ?-better_add(2,4,A). A=6. ?-better_add(3,A,5). A=2. better_add(X,Y,S) :- \+ var(X), \+ var(Y), var(S), S is X+Y. better_add(X,Y,S) :- \+ var(X), var(Y), \+ var(S), Y is S-X. better_add(X,Y,S) :- var(X), \+ var(Y), \+ var(S), X is S-Y. better_add(X,Y,S) :- \+ var(X), \+ var(Y), \+ var(S), S2 is X+Y, S=S2.

    7. Examples of Prolog list notation [a,b,c]: a list of three constants a, b, and c [a,b,X]: a list of three items a, b, and some unspecified item represented by a variable X [X | Y]: a list whose first item is represented by variable X, and whose rest-of-list is represented by variable Y (Y is a list of zero or more items) [a,b,c | Y]: a list whose first three items are a,b,c, and whose rest is represented by some variable Y [ ]: the empty list (a list of no items)

    8. Example queries with lists Database: employees([tom, dick, harry, mary]). Queries: ?- employees(L). L=[tom, dick, harry, mary] ?- employees([X | L]) X = tom, L = [dick, harry, mary] ?- employees([X, Y, Z | L]). X = tom, Y = dick, Z = harry, L = [mary] ?- employees([X, dick | L]). X = tom, L = [harry,mary] ?- employees([X, tom | L]). no ?- employees([X, Y, Z, W | L]) X=tom, Y=dick, Z=harry, W=mary, L=[ ] ?- employees([X, Y, Z, W, A | L]) no ?- employees(L), write([joe, jim | L]), nl. [joe,jim,tom,dick,harry, mary] L=[tom,dick,harry,mary] ("nl" does carriage return)

    9. Classic list-processing programs

More Related