Download presentation
Presentation is loading. Please wait.
Published byCaroline Andersen Modified over 6 years ago
1
Chapter 3: Prolog (Lists, Arithmetic, Operators)
DM552: Part 2 Programing Logic Chapter 3: Prolog (Lists, Arithmetic, Operators) Dr Youcef Djenouri
2
Lists
3
Definitions (1 /3) The list is a simple data structure used in non-numeric programming. An empty list written as [ ] is considered as atom. Lists are contained in square brackets with the elements being separated by commas. - [Dansk, tennis, swimming, school] Elements of list could be any valid Prolog terms, i.e., atoms, numbers, variables, or compound terms. - [tennis, [], Y, mother(Y, Jenny), [a, b], f(a, 2)]
4
Definitions (2 /3) Lists are represented as compound terms using the functor . (dot): - The list [a, b, c] corresponds to the term: .(a, .(b, .(c, []))) The first element of a list is called its head and the remaining list is called the tail. An empty list doesn't have a head. A list just containing a single element has a head and its tail is the empty list
5
[Jenny, Marco, Mohamed, Mounir, John]
Definitions (3 /3) Functor | (bar) can separate between head and tail of a list: - [1, 2, 3, 4, 5] = [X | Y]. X = 1 Y = [2, 3, 4, 5] Exercise: Identify the second element of the following list: [Jenny, Marco, Mohamed, Mounir, John] Solution: [_, X | _].
6
Manipulation: Membership
member(X, L): True if X is member of L False Otherwise
7
Code Prolog member(X,[X|Tail]).
member(X,[Head|Tail]):- member(X,Tail).
8
Manipulation: Concatenation
append(L1, L2, L3):True if L3 is the concatenation of L1 and L2. False, otherwise.
9
Code Prolog append([],L,L).
append([X|L1],L2,[X|L3]):- append(L1,L2,L3).
10
Examples ?-append([a,b,c], [d,e,f],[a,b,c,d,e,f]). Yes ?-member(a,[d,e,f]). no ?-member(X, [a,b,c]). X=a X=b X=c
11
Arithmetic and Operators
12
Prolog arithmetic Prolog arithmetic can be a little surprising
?-X = X = 3+4 ; 3 + 4 is just a term whose functor is + and arguments are 3 and 4
13
Predicate is (1/2) Predicate is performs arithmetical operations as a side effect The logical value of is is always true A correct usage: X is expression where expression contains only numerical constants (or variables instantiated to numerical constants)
14
Predicate is (2/2) The is operator will force evaluation. So the right way to invoke arithmetic is: ?-X is 3+4. Now the answer will be: X=7 The addition here was carried out by a special procedure that is associated with the operator +. We call such procedures built-in procedures
15
Arithmetic Operators (1/2)
X+Y, the sum of X and Y X -Y, the difference of X and Y X*Y, the product of X and Y X/Y, the quotient of X and Y X^Y, X to the power of Y -X, the negative of X
16
Arithmetic Operators (2/2)
abs(X), the absolute value of X sin(X), the sine of X (for X measured in degrees) cos (X), the cosine of X (for X measured in degrees) max(X,Y), the larger of X and Y sqrt(X), the square root of X
17
Comparison Predicates
X =:= Y the values of X and Y are equal. X =\= Y the values of X and Y are not equal. X < Y X is less than Y. X > Y X is greater than Y. X =< Y X is less than or equal to Y (sometimes <= ...). X >= Y X is greater than or equal to Y.
18
Define Predicates Define double and add 2
DoubleAddTwo(X,Y) :- Y is X*2+3. ?- DoubleAddTwo(2,X). X = 7 ?- DoubleAddTwo(3,X). X = 9
19
Equality operators (1/4)
Arithmetic Expression Equality E1=:=E2 succeeds if the arithmetic expressions E1 and E2 evaluate to the same value. ?- 6+4=:=6*3-8. yes ?- sqrt(36)+4=:=5*11-45.
20
Equality operators (2/4)
Arithmetic Expression Inequality =\= E1=\=E2 succeeds if the arithmetic expressions E1 and E2 do not evaluate to the same value. ?- 10=\=8+3. Yes
21
Equality operators (3/4)
Term Identical: Both arguments of the infix operator == must be terms. The goal Term1==Term2 succeeds if and only if Term1 is identical to Term2. Any variables used in the terms may or may not already be bound, but no variables are bound as a result of evaluating the goal. ?- likes(X,prolog)==likes(X,prolog). X = _ ?- likes(X,prolog)==likes(Y,prolog). no ?- 6+4==3+7.
22
Equality operators (4/4)
Terms Not Identical \== Term1\==Term2 tests whether Term1 is not identical to Term2. The goal succeeds if Term1==Term2 fails. Otherwise it fails. ?- pred1(X)\==pred1(Y). X = _ , Y = _ (The output signifies that both X and Y are unbound and are different variables.)
23
Operator Precedence When there is more than one operator in an arithmetic expression, e.g. A+B*C-D, Prolog needs a means of deciding the order in which the operators will be applied. Prolog achieves this by giving each operator a numerical precedence value. Operators with relatively high precedence such as * and / are applied before those with lower precedence such as + and -. Operators with the same precedence (e.g. + and -, * and /) are applied from left to right.
24
Input/Output Data A Prolog program can read data from input data, and write data to output data. Data are associated with files. Data coming from the user's terminal is treated as just another input data. Data output to the terminal is treated as another output data.
25
Write() ?- write([a,b,c,d]) [a,b,c,d] ?- write(a) a ?- write(Hello World) syntax error ?- write(‘Hello World’) Hello World
26
Formatting Output nl: write a new line to the current output. tab(i): write a i number of white spaces to the current output.
27
read() We can read terms from the terminal using the command read.
This displays a prompt ‘|:’ and waits for the user to input a term followed by a full-stop. ?- write('What is your name?'), nl, read(X). What is your name? |:
28
Reading input read() can only recognize Prolog terms finished with a
full-stop. ?- read(X). |: hello. Strings of text must be enclosed in single quotes. ?- read(X) ?-read(X) |: Hi there! |: ‘Hi there!’, syntax error X=‘Hi there!’? yes
29
Usage of Quotes (1/2) When we are reading strings there are two ways we can input them: if we enclose them in single quotes (e.g. ‘Hi Bob!’) the string is read verbatim. ?- read(X). |: 'Hi bob!'. X = 'Hi bob!' ? yes
30
Usage of Quotes (2/2) 2. if we enclose them in double quotes (e.g. “Hi Bob!”) the string is interpreted into the corresponding list of ASCII codes. ?- read(X). |: "Hi bob!". X = [72,105,32,98,111,98,33] ? yes
31
ClassWork Write Prolog code that display the following text: a b c d e
32
Response ?- write(a), tab(3), write(b), nl, tab(1), write(c), tab(1), write(d), nl, tab(2), write(e).
33
ClassWork length(L,X): True, if X is the number of elements of the list L. False, otherwise.
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.