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.
Related
I would like to ask if you could advise me how to create a condition or redo the code. I need the output of the program to detect the symptoms of covid19. If I mark 'yes' at least 3 times out of ten symptoms in the program, the output will be 'you have covid19 symptoms'. If 'yes' less than 3 times, the output will be 'you have no covid19 symptoms'.
Thank you
start :- x(Disease), write('the verdict is : '), write(Disease),nl, undo.
x(symptoms) :- symptoms,!.
x(withoutsymptoms). /* without symptoms */
symptoms :- verify(conjunctivitis),
verify(sore_throat),
verify(fatigure),
verify(shake),
verify(diarrhea),
verify(runny_nose),
verify(cold),
verify(muscle_pain),
verify(chest_pressure),
verify(loss_of_taste),
verify(fever).
ask(Question) :-
write('How are you? '),
write(Question),
write('? '),
read(Response),
nl,
( (Response == yes ; Response == yes)
->
assert(yes(Question)) ;
assert(no(Question)), fail).
:- dynamic yes/1,no/1.
verify(S) :-
(yes(S)
->
true ;
(no(S)
->
fail ;
ask(S))).
undo :- retract(yes(_)),fail.
undo :- retract(no(_)),fail.
undo.
That attempt looks confusing. How about the following, which additionally doesn't use assertz/1 to poke the Prolog database, but collects the responses in a list for later analysis.
Also, instead of using the "term deserializer" read/1, we use read_line_to_string/2
% ---
% Symptoms are listed naturally in a list.
% There are 11 symptoms
symptoms( [conjunctivitis,
sore_throat,
fatigue,
shake,
diarrhea,
runny_nose,
cold,
muscle_pain,
chest_pressure,
loss_of_taste,
fever] ).
% ---
% Go through the symptoms list, collecting answers
ask_about_symptoms([],[]).
ask_about_symptoms([Symptom|MoreSymptoms],[Answer-Symptom|MorePairs]) :-
repeat,
format("Do you have: ~s?\n",[Symptom]),
read_line_to_string(user_input,S1),
string_lower(S1,S2),
(
member(S2,["yes","y","ja","oui"])
->
Answer = yes
;
member(S2,["no","n","nein","non"])
->
Answer = no
;
format("Please try again\n",[]),fail % Back to "repeat"
),
ask_about_symptoms(MoreSymptoms,MorePairs).
is_yes_pair(yes-_).
ask_candidate :-
symptoms(Symptoms),
ask_about_symptoms(Symptoms,AnswerPairs),
!, % don't go back to asking
include(is_yes_pair,AnswerPairs,Included),
format("So you say you have the following symptoms: ~q~n",[Included]),
length(Included,Hits),
((Hits>=3) -> format("Looks bad.~n",[]) ; format("You probably don't have it.~n",[])).
Example run:
?- [rona].
true.
?- ask_candidate.
Do you have: conjunctivitis?
|: y
Do you have: sore_throat?
|: y
Do you have: fatigue?
|: n
Do you have: shake?
|: n
Do you have: diarrhea?
|: n
Do you have: runny_nose?
|: y
Do you have: cold?
|: foo
Please try again
Do you have: cold?
|: bar
Please try again
Do you have: cold?
|: n
Do you have: muscle_pain?
|: n
Do you have: chest_pressure?
|: y
Do you have: loss_of_taste?
|: y
Do you have: fever?
|: y
So you say you have the following symptoms: [yes-conjunctivitis,yes-sore_throat,yes-runny_nose,yes-chest_pressure,yes-loss_of_taste,yes-fever]
Looks bad.
true.
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]).
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 write:
if (KSO<20) then {
printf( "Is there NO3 in the water (yes/no)");
scanf("%s",NUMKSOVALUE_value);
}
in prolog.
By now i have written :
write('Is there NO3 in the water (yes/no)?'),
nl, read(NUMKSOVALUE_value),
member(NUMKSOVALUE_value,[yes,no]). :- (number(KSO_labvalue),KSO_labvalue < 20).
But it is not accepted. Can someone help me please with this?
A good place to start learning Prolog is: http://www.learnprolognow.org/
For your question, the following code will help get you started:
no3_present_in_water :-
write('Enter KSO value:'), nl,
read(V),
process_response(V,Outcome),
write('NO3 present: '), write(Outcome), nl.
process_response(A,B) :-
A =< 20,
B = 'no'.
process_response(A,B) :-
A > 20,
B = 'yes'.
Test:
?- no3_present_in_water.
Enter KSO value:
|: 10.
NO3 present: no
yes
?- no3_present_in_water.
Enter KSO value:
|: 30.
NO3 present: yes
yes
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)...
...