How do I convert a string into ascii in amzi! prolog? - prolog

so I tried converting strings into ascii with the name/2 and char_codes/2 and so on, but I keep getting things like [0w0061,0w0062,0w0063]
for example.
?- name(abc,A).
A = [0w0061,0w0062,0w0063]
I want the returned list to be [97,98,99].
any help?

ok so I got it.
if I tried for example
?- name(abc,A).
and it returned this:
A = [0w0061,0w0062,0w0063]
I took a returned member and subtracted 0 from it. It looks like this.
for example:
?- name(a, A).
A = [0w0061] ,
no
?- A is 0w0061 - 0.
A = 97

Related

When the prolog returns sat as an output what does it mean?

When I run my code it returns the sat as the output ? Is it trying to convey an error or what does the output means ?
%Scenario 2:
%Amy: “My report is original.”
%Brian: “Mine as well”
report(2, [Amy,Brian]) :-
sat(Amy=:=Amy),
sat(Brian=:=Brian),
write('1 they are telling the truth , 0 they are lying').
The output that I get is :-
It is not trying to convey you an error, it is being as specific as it can be. That is sat(Amy=:=Amy), sat(Brian=:=Brian) is satisfiable whenever sat(Amy=:=Amy) and sat(Brian=:=Brian).
CLP(B) does not automatically designate variables in expressions to be boolean. You can use labeling(+Vs) to inform it as follows:
?- use_module(library(clpb)).
?- sat(Amy=:=Amy), sat(Brian=:=Brian), labeling([X,Y]).
X = Y, Y = 0,
sat(Amy=:=Amy),
sat(Brian=:=Brian)
This is only the first solution you can use backtrack to obtain the others.

Problem in performing translation of list of numbers to words such as [1,2,3] to [one,two,three]

Hello everyone I am trying to translate numbers to list of words. My logic seems to be correct but there is something wrong with the output. I am using swi-prolog in my Mac.
So when I enter
translate([1,2,3],X).
it gives false.
means(0,zero).
means(1,one).
means(2,two).
means(3,three).
means(4,four).
means(5,five).
means(6.six).
means(7,seven).
means(8,eight).
means(9,nine).
translate([],[]).
translate([Head|Tail],[Head1|Tail1]):-
means(Head,Head1),
translate(Tail,Tail1).
Expected:
?- translate([1,2,3],X).
X = [one,two,three].
But got:
?- translate([1,2,3],X).
false.

Prolog programm returns yes instead of value

I got the following event: item(C,X,G,P), (where C is a number for the product,X it's name,G it's price,P it's cost).
When i use the command item(n3001,_,_,P) directly on the prolog console i get as answer
G = 1.25 X = 100 but when i write the equation p3(C)-: item(C,_,_,P). then i consult the text i get as answer yes.
My question clarified is how come the one time i get the value of P which i want and the other time i get whether it's true or false?
There are no return values in Prolog and p3/1 does not constitute a function but a relation. Your definition
p3(C) :-
item(C,_,_,P).
reads: If item(C,_,_,P) succeeds then p3(C) succeeds as well. For the sake of argument, let's assume that your code includes the following fact:
item(n3001,100,1.25,1).
If you query
?- p3(n3001).
Prolog unifies C in the head of your rule with n3001 and then tries your goal item(C,_,_,P) which succeeds. Hence the rule succeeds and Prolog tells you:
?- p3(n3001).
yes
If you want to know the price corresponding to n3001 you have to to define a rule where P appears in the head of the rule as well, e.g.:
p3(C,P) :-
item(C,_,_,P).
If you query that you'll get to see the value of P corresponding to n3001:
?- p3(n3001,P).
P = 1
If you query item/4 directly P appears in the arguments and therefore you get to see a substitution for it that satisfies your query:
?- item(n3001,_,_,P).
P = 1

Prolog not showing full list of answer

When I run query human(Who). on the below .pl file
human(ann).
human(george).
human(mike).
I only get back Who = ann .
Instead of
Who = ann ;
Who = george ;
Who = mike.
Am using prolog 6.6.6. How do I get it to show the full list?
The answer you got was the following. Do you note the space before the dot?
Who = ann .
^ SPACE!!!
This space means: The query was aborted. Maybe you typed return. Or maybe you have a somewhat illconfigured terminal.
To better check this, try:
?- X = 1 ; X = 2 ; X = 3.
There you should get all three answers, too. If not, it is definitely your terminal
What you are seeing is the default behaviour of prolog.
The query
?- findall(Object,Goal,List).
Should work for you.
Eg.
findall(X, human(X), L).
It will populate the list with all possible answers.

convert compound to atom in prolog

I want to use atom_chars/2 on the expression of 3+4, but I get
ERROR: atom_chars/2: Type error: 'atom' expected, found '3+4' (a compound).
I'm thinking that if I can add " " on both sides of the compound, it would work, e.g.
atom_chars("3+4", Result).
but I don't know how I can do that, or is there other approaches to do this?
Please give me some advice.
EDIT: What I mean is that the input has to be 3+4, instead of '3+4', so what I want to do is to write a predicate before the atom_chars/2 to convert 3+4 to '3+4'.
For instance: for compound2atom(X,Y),
-?compound2atom(3+4,Y).
Y='3+4'.
If you are using SWI-Prolog, there is with_output_to/2 or format/3:
?- with_output_to(atom(A), write(3+4)).
A = '3+4'.
?- with_output_to(chars(C), write(3+4)).
C = ['3', +, '4'].
?- format(atom(A), "~w", [3+4]).
A = '3+4'.
?- format(chars(C), "~w", [3+4]).
C = ['3', +, '4'].
But if you look hard enough you should be able to find some predicate that does that, for example term_to_atom/2.
My personal preference leans towards format/3.

Resources