Programming Paradigms CSI2120: Jochen Lang EECS, University of Ottawa Canada
Programming Paradigms CSI2120: Jochen Lang EECS, University of Ottawa Canada
Programming Paradigms CSI2120: Jochen Lang EECS, University of Ottawa Canada
Paradigms
CSI2120
Jochen Lang
EECS, University of Ottawa
Canada
Arithmetic Expressions and I/O
• Arithmetic Expressions
– Built-in operators
– Unification with numbers
– Recursive calculations
– Looping with repeat
– Generator
• Input and output: Streams
– Reading and writing to console
– Reading and writing to file
– Character i/o
• Comparisons
X =:= Y % X equals Y
X =\= Y % X not equals Y
X < Y
X =< Y
X > Y
X >= Y
• The operators are applied after calculations, e.g.,
?- 1+2 =:= 2+1.
true.
emperor(Name,Year) :- reigns(Name,X,Y),
X=<Year,
Y>=Year.
weight(joe,70).
height(joe,1.90).
bmi(Person, Index) :- weight(Person,P),
height(Person,T),
Index is P/(T*T).
min(X,Y,X) :- X<Y.
min(X,Y,Y) :- X>=Y.
• What queries can we ask?
?- min(5,7,X).
?- min(5,X,7). % false
?- min(X,5,7). % false
?- min(X,Y,7). % error – why?
• Positive Powers
– boundary case for power to 1
pow( X, 1, X ).
– recursion to calculate the product
pow( X, Y, Z ) :- Y > 1,
Y1 is Y - 1,
pow( X, Y1, Z1 ),
Z is X * Z1.
• Factorial
– boundary case for factorial of zero
fact(0, 1).
– recursion to calculate the product via the next smaller
factorial
fact(N, F) :- N > 0,
N1 is N – 1,
fact(N1, F1),
F is F1 * N.
gcd(1071,462,W) :-
462>0, 147 is 1071 mod 462,
gcd(462,147,W) :-
147>0, 21 is 462 mod 147,
gcd(147,21,W) :-
21>0, 0 is 147 mod 21,
gcd(21,0,W).
W = 21.
• Fibonacci numbers
– a series of numbers 1 1 2 3 5 8 13 21 …
– Recursive clause based on Fibonacci’s algorithm
• fib(N) = fib(N-1) + fib(N-2)
fib(N,F):- N>1,
N1 is N-1,
N2 is N-2,
fib(N1,F1),
fib(N2,F2),
F is F1+F2.
– Two boundary conditions are needed.
fib(0,1).
fib(1,1).
• Reading terms:
• read/1 is for input from the currently open stream.
– The term has to be followed by a . (dot) and return at which
point the read goal will succeed and X will be instantiated to
the entered characters.
– The prompt is system dependent, e.g., a : (colon).
• Example :
?- read(X).
|: a(1,2).
X = a(1,2)
• Calculator
– if end test fails, we backtrack until repeat succeeds again
• The “Cut “ ! stops backtracking across it
– More details in the next lecture
• Calculator
– if end succeeds, we don’t backtrack across it to find more
solutions
• Predicate open/3
– argument 1: Filename
– argument 2: File mode: write, append or read
– argument 3: Instantiated with the name of the stream (file
handle) that must be used to manipulate the stream status
(close, set_input, etc.)
• Modes for writing:
– write mode opens the file and puts the stream marker at
the beginning of the file.
• existing content is overwritten
– append mode puts the stream marker at the end of the file
• Predicate close/1
– takes a file handle and closes the stream
• The current input and output stream can be set, affecting all
input and output commands (e.g., read, write, etc.)
– set_input(X)
• user_input is the keyboard
• Query with current_input(X)
– set_output(X)
• user_output is the console
• Query with current_output(X)
• All the read and write predicates can take an extra
parameter for the file handle
– write(X, Y). X is the file handle (as above)
– read(X,Y) get(X, Y) get0(X,Y)
Write X to file
writeFile(X):- open('test.txt', append, F),
write(F, X), nl(F),
close(F).
answer(stop) :- write('Exiting'),nl.
answer(Province) :- capital(Province,City),
write(City),write(' is the capital of '),
write(Province),nl,nl,
askP.
• Arithmetic Expressions
– Built-in operators
– Unification with numbers
• Recursive calculations
• power, factorial, gcd, fibonacci
• crossed recursion
• Generator
• Looping with Repeat
• Input and output: Streams
– Reading and writing to console
– Reading and writing to file
– Character i/o