8 Prolog4 Input Output
8 Prolog4 Input Output
Programming Paradigms
1
• Course overview
• Introduction to programming
paradigms
• Review: The object-oriented
Topics paradigm in Java
• Imperative and concurrent
programming paradigm: Go.
• Logic paradigm: Prolog.
• Functional paradigm: Scheme.
• Office hours offered from Feb 22
– March 1st (please check
Announcement Brightspace and the
announcement)
• Thursday Feb 23 (4:00 -5:20 pm)
live Tutorial session for assignment
1 go/ comprehensive assg_part1
Announcement Go Concurrent
• (TA: Bamdad)
• Comprehensive
assigenemnt_part1 Go -> due date
extended to Friday Feb 26th
Announcement • TA (Bamdad) will help and mark
it
• Assignment 1 (GO) will be posted
tonight -> Due date March 1st
Announcement • TA Ooha, Hari
Input -Output
Example of use :
? - calculator.
: 2 + 3.
5
: 3 + 2 * 4 -1.
10
: end.
end
YES
2021-02-23
Control of Backtracking in Calculator
• 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
Opening and Closing a File
• 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
Reading and Writing
• 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)
Example: Write to File
Write X to file
writeFile(X):- open('test.txt', append, F),
write(F, X), nl(F),
close(F).
Default Input and Output Stream
? - start.
The Capitals of Canada
Province? ontario.
the capital of ontario is toronto
Province? quebec.
the capital of quebec is quebec
Province? stop.
thank you
true.
Summary