I have some prolog codes, I print processes on terminal. Also I write to file, what I print on terminal. I put write to file functs, near every write funct. But they are not same. Writing file is not working well.
printrow([]) :- write(' '),
open('output.txt',append,OS), write(OS,' '), close(OS).
% I try to put same process for file
printrow([X|Xs]) :- printrepl(X,Y), write(' '), write(Y),
printrow(Xs), open('output.txt', append,OS),
write(OS,' '), write(OS,Y), close(OS).
printrepl(' ',' ') :- !.
printrepl(x,'X').
printrows([]) :- nl.
printrows([N|Ns]) :- write(N), write(' '),
open('output.txt', append,OS), write(OS,N), write(OS,' '),
printrows(Ns), write(OS,' \n'), close(OS).
% I can't find alternative for writef('%2r w',[X]) below,
% for writing file, I think there is bug
writek(K,List) :- nth1(K,List,X), !, writef('%2r w',[X]),
open('output.txt',append,OS), write(OS,' '), write(OS,X), close(OS).
writek(_,_) :- write(' '),
open('output.txt',append,OS),write(OS,' '), close(OS).
Can you give me advice for writing same outputs with terminal?
Here is a rewrite for writek and printrows. This could be of use.
% ---
% Printing rows (as "~w", equal to write/1) to stream Stream
% ---
printrows([],Stream) :-
format(Stream,"~n",[]).
printrows([N|Ns],Stream) :-
format(Stream,"~w ~n",[N]),
printrows(Ns,Stream).
% ---
% Printing/Appending Rows to the file named by Filename
% ---
printrows_to_file(Filename,Rows) :-
setup_call_cleanup(
open(Filename, append, Stream), % NB append
printrows(Rows,Stream),
close(Stream)).
% ---
% Printing Rows to stdout
% ---
printrows_to_stdout(Rows) :-
printrows(Rows,user_output).
% ---
% Write "w", indented by 1-based value of List[K]
% ---
writek(K,List,Stream) :-
nth1(K,List,Element),
!,
forall(between(1,Element,_),format(Stream," ",[])), % Indent
format(Stream,"w~n",[]).
writek_to_file(Filename,K,List) :-
setup_call_cleanup(
open(Filename, append, Stream), % NB append
writek(K,List,Stream),
close(Stream)).
writek_to_stdout(K,List) :-
writek(K,List,user_output).
For example:
?- printrows_to_stdout([1,2,3]).
1
2
3
true.
?- writek_to_stdout(1,[4,5,6]).
w
true.
Similarly for
?- printrows_to_file('filename.txt',[1,2,3]).
?- writek_to_file('filename.txt',1,[4,5,6]).
Related
I am trying to create a prolog program to print out the truthtable of a statement. It works fine for the example truthtable(A,B,and(A,B)),
but if I try truthable(A,B,or(A,and(A,B))) it doesn't work and shows everything as false.
and(true,true):- true.
and(false,true):-false.
and(true,false):-false.
and(false,false):-false.
or(true,true):-true.
or(false,true):-true.
or(true,false):-true.
or(false,false):-false.
non(true):-false.
non(false):-true.
evaluate(E, true) :- E, !.
evaluate(E, false).
bool(true).
bool(false).
truthtable(A,B,E):-
bool(A),
bool(B),
write(A),
write(' \t '),
write(B),
write(' \t '),
evaluate(E, R),
write(R),
nl,
fail.
Also what can I do if I want the user to add any number of inputs for example A,B and C not only A and B.
I want to read a text file using Prolog and then append that file. I used following code to do that.
readWord(InStream,W) :-
get0(InStream,Char),
checkCharAndReadRest(Char,Chars,InStream),
atom_chars(W,Chars), write(W),write('\n').
checkCharAndReadRest(10,[],_) :- !. % Return
checkCharAndReadRest(32,[],_) :- !. % Space
checkCharAndReadRest(-1,[],_) :- !. % End of Stream
checkCharAndReadRest(end_of_file,[],_) :- !.
checkCharAndReadRest(Char,[Char|Chars],InStream) :-
get0(InStream,NextChar),
checkCharAndReadRest(NextChar,Chars,InStream).
writeWord(end_of_file).
writeWord(X) :-
write(X),nl.
readFile:-
open('output.txt', read, In),
repeat,
readWord(In,W),
writeWord(W),
W == end_of_file, !,
close(In).
adding :-
open('output.txt', append, In),
write(In, 'abc'),
close(In).
Example:
Text file data -
Ann
csu2280
23
end_of_file
Required output –
saman
csu2280
23
abc
end_of_file
But I got the answer as
saman
csu2280
23
end_of_fileabc
How can I get the required answer?
menu :-
repeat,
write(' '),nl,
write(' 1.the number of hello '),nl,
write(' '),nl,
write('enter your choice:'),nl,
read(Choice), Choice>0, Choice =<6,
doit(Choice),Choice=6.
doit(1):- numberofhello(N).
doit(6):- abort.
my_list([hello,hello,hello]).
counthowmany(_, [], 0) :- !.
counthowmany(X, [X|Q], N) :- !, counthowmany(X, Q, N1), N is N1+1.
counthowmany(X, [_|Q], N) :- counthowmany(X, Q, N).
numberofhello(N) :- my_list(L),counthowmany(hello,L,N).
now in the code above after compile buffer when I ask Prolog for a menu the menu appears
with one choice and when i enter 1 for number of hello in the list(my_list) i don't get any answer and i get a singleton variable warning while compiling .....
can anyone help me please........
A "singleton" variable is very similar, in other languages, to the warning, "Variable was assigned a value that is never used" or "A variable has been declared but never used". So your clause must do something with N in your first doit clause. You can also avoid the doit(6) :- abort. clause by allowing your menu to succeed without any choice points using a cut (!).
So, try:
menu :- repeat,
write(' '),nl,
write(' 1.the number of hello '),nl,
write(' '),nl,
write('enter your choice:'),nl,
read(Choice), Choice>0, Choice =<6,
doit(Choice), Choice=6, !.
doit(1):-
numberofhello(N),
write('Number of hello is = '), write(N), nl.
% Removed doit(6)...
...
checkChar :-
nl,
write('Enter a character [press 0 to stop]: '),
get(X),
process(X).
process(X) :-
S = put(X),
0 == S,
!.
process(X) :-
write('ASCII code for <'),
put(X),
write('>:'),
write(X),
checkChar.
User will input anything they want, the prolog will translate the character into the ASCII code and display it.
The prolog will stop to execute if inputted 0, but how can i do that other than straight away compare with ASCII 48? (ASCII 48 = 0)
This is what i had tried, but it still can't stop once i enter 0.
I think this is what you may be looking for:
checkChar :-
nl,
write('Enter a character [press 0 to stop]: '),
get(X),
process(X).
process(X) :-
X =\= "0",
write('ASCII code for <'),
put(X),
write('>:'),
write(X),
checkChar.
For example I have:
pos(10, 20).
How can i write the predicate which returns the first pos term(10).
get_pos_x(Pos) :- % should return the first pos param(10).
Example of work:
get_pos_x(pos(10,20)) :- % should write 10.
There is a difference between a predicate pos(10, 20). and a term pos(10, 20).
For the predicate, this would be the code:
pos(10, 20).
And this would be the execution of the code:
:- pos(X, 20), write(X), nl.
For the term, this would be the code:
get_pos_x(pos(X, _)) :-
write(X), nl.
And this would be the execution of the code:
:- get_pos_x(pos(10, 20)).