Input -Output
• Readingandwritingtoconsole • Readingandwritingtofile
• Characteri/o
Copyright By PowCoder代写 加微信 powcoder
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(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.
• Reading terms:
• read/1 is for input from the currently open
– 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) :-
write(‘Give the age of’), write(X), write(‘: ‘), read(Y).
?- age(teddy, Z).
Give the age of teddy: 22. Z = 22
?- age(teddy, 22).
Give the age of
teddy: 23.
?- read(X + Y). :2 + 3.
Repeat Predicate (built-in)
• Thebuilt-inpredicaterepeatisawaytogenerate multiple solution through backtracking.
• Definition
repeat :- repeat.
test :- repeat,
write(‘Answer to everything? (num)’), read(X),
Calculator Example:
• Readanarithmeticexpressionfromastream • Calculateresult
• Exitonend
calculator :- repeat, read(X),
eval(X,Y), write(Y),nl, Y = end, !.
% loop forever
% read expression
% our evaluation %outputresult
% 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.
: 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“!stopsbacktrackingacrossit
– 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
• Predicateopen/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.)
• Modesforwriting:
– 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
• Thecurrentinputandoutputstreamcanbeset, 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)
• Allthereadandwritepredicatescantakeanextra 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),
Default Input and Output Stream
• Alternative(simpler)waystosetthecurrentinputand 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)getsacharacter intothecurrent
– 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.
• Inputandoutput:Streams
– Reading and writing to console – Reading and writing to file
– Looping with Repeat
– Character i/o
程序代写 CS代考 加微信: powcoder QQ: 1823890830 Email: powcoder@163.com