Download presentation
Presentation is loading. Please wait.
1
Input and Output and Built_in Predicates
Hicran ŞEVİK Ahmet Emre ÇETİN
2
Input Predicates readln readchar readint readreal
3
Output Predicates writedevice write writef
4
Reading and Writing Terms
Reading Terms read(X): The special predicate read will read the next term that you type in from the computer terminal's keyboard. The term must be followed by a dot and a non-printing character such as a space or a new line. If x is unistrantiated , the goal read(x) will cause the next term to be read , abd X to be instantiated to the term. Writing Terms write (X) : A predined predicate that output a term X to the output stream from user input stream (keyboard or from a file).
5
ReadLine and Write Predicate
address(City,"Ontario"):- write("Does the "+City+" belond to Ontario(yes/no)?"), readln(Reply),nl, write("Right answer is :"). address("Las Vegas","California"). address("London","Ontario"). address("Istanbul","Turkey").
6
ReadChar and WriteChar Predicate
address(City,"Ontario"):- write("Does the"+City+"belond to Ontario(yes/no)?"), get_char(Reply),nl, write("your answer is :"), put_char(Reply),nl, write("Right answer is :"). address("Las Vegas","California"). address("London","Ontario"). address("Istanbul","Turkey").
7
ReadInt and Write Predicate
chkage(Patient):- write("What is",Patient,"s Age?"), readint(Age),nl, Age>12,nl, Write("Patient cannot be evaluated."),nl. chkage("James"). chkage("Alex").
8
ReadReal and Write predicate
askprice(Item,Price):- write("What is the price of ",Item,"?"), readread(Price),nl, write(Item "Price is",Price).
9
Writef predicate Formatted write. Format is an atom whose characters will be printed. Format may contain certain special character sequences which specify certain formatting and substitution actions. Arguments provides all the terms required to be output. likes(ahmet,cricket). likes(hicran,cricket). likes(ahmet,football). likes(hicran,snooker). likes(ahmet,computergames). likes(hicran,tennis). run:- writef("Enter Person Name="), read(Person),nl, likes(Person,X), writef("%t likes %t \n" , [Person,X]), fail.
10
Input Output Handling Using a Data File in Prolog
Methots: Open() Close()
11
Input and Output Streams
At the beginning of execution,the input stream and output stream correspond to the user terminals(keyboard and screan). The goal:see(FileName). Causes the input to be switched from the previous input stream to FileName.Therefore ,Prolog is ready to read the input from FileName. kb1.pl too_easy(prolog). boring(X):-too_easy(X). … ?-see(‘kb1.pl’). yes. ?-read(X). X=too_easy(prolog) ?-see(user).
12
Input and Output Streams
The goal: tell(FileName). causes the output to be switched to FileName instead of the previous output stream. Therefore,Prolog is ready to display the output to FileName. kb2.pl like(ali,garfield). ?-tell(‘kb2.pl’). yes. ?-write(‘likes(ali,garfield).’). ?-tell(user).
13
Build-In Predicates To make Prolog programming more practical, a number of predicates or are built into Prolog. These include some utility procedures, which could have been programmed by the Prolog user, and some which do non-logic programming things, like the input and output routines, debugging routines, and a range of others. These functions should be used in a context where they will actually be evaluated, such as following is or as part of an arithmetic comparison operator like =:= or >.
15
THANKS FOR LISTENING Ahmet Emre ÇETİN Hicran ŞEVİK
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.