0% found this document useful (0 votes)
37 views23 pages

8 Prolog4 Input Output

This document discusses input and output in Prolog. It covers reading and writing to the console and files, setting the input and output streams, character input/output, and provides examples of writing to a file and a simple question/answer program that reads provinces and outputs their capitals. Control structures like repeat and cut are also introduced for looping and stopping backtracking in programs.

Uploaded by

dongmianjun
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
37 views23 pages

8 Prolog4 Input Output

This document discusses input and output in Prolog. It covers reading and writing to the console and files, setting the input and output streams, character input/output, and provides examples of writing to a file and a simple question/answer program that reads provinces and outputs their capitals. Control structures like repeat and cut are also introduced for looping and stopping backtracking in programs.

Uploaded by

dongmianjun
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 23

CSI2120 A

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

• Reading and writing to console


• Reading and writing to file
• Character i/o
Input-Output

• Write to the screen or to a file


• Read from the keyboard or from a file
• Writing terms with the built-in predicate
write/1
– write(X). adds the value of X to the
currently active output stream (by default
the console).
– Example:
• write(1+2) outputs 1+2
– nl is the new line command, i.e.,
• writeln(X) :- write(X), nl.
– tab(N) outputs N spaces
More Output Commands

• write/1 vs. display/1


– Both, write and display output to the
current streams
– write displays operators as operators
– displays ignores all operator definitions
– Example:
write(3+4), nl, display(3+4), nl.
• Output:
3+4
+(3,4)
YES
Input
• 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)
Interactive Example
age(X, Y) :- ?- age(teddy, 22).
write('Give the age of'), Give the age of
teddy: 23.
write(X), write(': '),
No
read(Y). ?- read(X + Y).
?- age(teddy, Z). :2 + 3.
Give the age of teddy: 22. X = 2
Z = 22 Y = 3
Yes Yes
Repeat Predicate (built-in)

• The built-in predicate repeat is a way to generate


multiple solution through backtracking.
• Definition
repeat.
repeat :- repeat.
• Example
test :- repeat,
write('Answer to everything? (num)'),
read(X),
(X=:=42).
Calculator Example:

• Read an arithmetic expression from a stream


• Calculate result
• Exit on end
calculator :- repeat, % loop forever
read(X), % read expression
eval(X,Y), % our evaluation
write(Y), nl, % output result
Y = end, !. % stopping condition

eval(end, end) :- !. % end evaluates to itself


eval(X, Y) :- Y is X. % otherwise calculate
The repeat

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

• Alternative (simpler) ways to set the current input and


output stream
– see(Filename). Filename becomes the current
output stream; opens file in write mode
– seen. Closes current output stream and reverts back
to the console.
– tell(Filename). Filename becomes the current
input stream
– told. Closes current input stream and reverts back
to the keyboard.
Character Input and Output

• put_char(Character) puts a character code into the


current stream
– character can either be an integer (e.g.,
ASCII_Code) or a character,e.g., ‘a’
– put(ASCII_Code) also exists as a non-ISO primitive
• get_char(Character) gets a character into the current
stream
– Non-iso primitives
• get0(X) unifies the variable X with the ASCII
code character entered .
• get(X) is the same as get0 but skips spaces.
Example: Province.pl
capital(ontario,toronto).
capital(quebec,quebec).

start :- write('The capitals of Canada'),nl,


askP.

askP :- write('Province? '), read(Province),


answer(Province).

answer(stop) :- write('thank you'),nl.


answer(Province) :- capital(Province,City),
write(City),write(' is the capital of '),
write(Province),nl,nl,
askP.
Example (continued)

? - 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

• Input and output: Streams


– Reading and writing to console
– Reading and writing to file
– Looping with Repeat
– Character i/o

You might also like