Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Prolog Read a function from a file 1

Status
Not open for further replies.

aa676

Programmer
Dec 13, 2009
12
GB
Hi, I need to build this program to read from a file sentences like "three plus zero." (all of them 3 words and just a simple operation) and return the result of the arithmetic operation. The solution I thought of was to use read_atom to store each word in X,Y,Z atoms. then make a list of them; so [X,Y,Z] is [three,plus,two], reverse order to [plus,three,two] and then using =.. convert that list to plus(three,two). I have defined (for example) three as number(three):- X = 3. and plus function as plus(number(X),number(Y)):- Z is X + Y. So now i have a file filled with functions such as plus(three,two). My question is how do I actually make the arithmetic process now? I'm totally stuck. any help would be much appreciated.

here is the code:

num(four):- X = 4.
num(five):- X = 5.
num(six):- X = 6.
num(seven):- X = 7.
num(eight):- X = 8.
num(nine):- X = 9.
num(ten):- X = 10.



plus(num(X),num(Y)):-
Z is X + Y.

minus(num(X),num(Y)):-
Z is X - Y.

times(num(X),num(Y)):-
Z is X * Y.

div(num(X),num(Y)):-
Z is X / Y.



my_word_calculator(In,Out):-
see(In),
tell(Out),
calculate,
told,
seen.


calculate:-
read_atom(X),
read_atom(Y),
read_atom(Z),
L =.. [Y,X,Z],

write(L).
 
It's just a way to do what you want, I'm not sure it's the best, and it doesn't do exactly what you want :
Code:
num(one,1).
num(two,2).
num(three,3).
num(four,4).
num(five,5).
num(six,6).
num(seven,7).
num(eight,8).
num(nine,9).
num(ten,10).

plus(X,Y, Z):-
	num(X, NX),
	num(Y, NY),
        Z is NX + NY.

minus(num(X),num(Y)):-
          Z is X - Y.

times(num(X),num(Y)):-
           Z is X * Y.

div(num(X),num(Y)):-
          Z is X / Y.

my_word_calculator(L):-
	calculate(L).

calculate(LM):-
	LM = [X,Y,Z],
        L =.. [Y, X, Z, T],
	call(L),
        write(T).
A result :
1 ?- calculate([one, plus, two]).
3
true.
It's written in SWI-Prolog :
 
thanks for the reply, sadly i need this to work in gprolog and use read_atom (which is not supported in swi-prolog) to read from a txt file.

so with your help i changed the code to this:

num(one,1).
num(two,2).
num(three,3).
num(four,4).
num(five,5).
num(six,6).
num(seven,7).
num(eight,8).
num(nine,9).
num(ten,10).


plus(X,Y,Z):-
num(X,NX),
num(Y,NY),
Z is NX + NY.

minus(X,Y,Z):-
num(X,NX),
num(Y,NY),
Z is NX - NY.

times(X,Y,Z):-
num(X,NX),
num(Y,NY),
Z is NX * NY.

divide(X,Y,Z):-
num(X,NX),
num(Y,NY),
Z is NX / NY.


my_word_calculator(In,Out):-
see(In),
tell(Out),

calculate,
told,
seen.


calculate:-
read_atom(X),
read_atom(Y),
read_atom(Z),
LM = [X,Y,Z],
L =.. [Y,X,Z,T],
call(L),
write(T).



but now prolog returns no...
 
fixed it, i was reading the number zero and i hadn't instantiated it. Thanks joel you were really helpfull ;)
 
ok , sorry about this, i got another problem. I am supposed to read several lines from the text, for example the txt file looks like:

three plus zero.
two times four.
stop.
two minus one.

so I need to make the program read all the lines until it finds a stop, and print the results in a new file. This is the code to do that:

num(zero,0).
num(one,1).
num(two,2).
num(three,3).
num(four,4).
num(five,5).
num(six,6).
num(seven,7).
num(eight,8).
num(nine,9).
num(ten,10).


plus(X,Y,Z):-
num(X,NX),
num(Y,NY),
Z is NX + NY.

minus(X,Y,Z):-
num(X,NX),
num(Y,NY),
Z is NX - NY.

times(X,Y,Z):-
num(X,NX),
num(Y,NY),
Z is NX * NY.

divide(X,Y,Z):-
num(X,NX),
num(Y,NY),
Z is NX / NY.


my_word_calculator(In,Out):-
see(In),
tell(Out),

calculate,
told,
seen.

calculate:-
repeat,
read_atom(X),
read_atom(Y),
read_atom(Z),
LM = [X,Y,Z],
L =.. [Y,X,Z,T],
call(L),
write(T),nl,
X = stop.


the txt file is exactly as I mentioned above. The problem is prolog returns this error:

| ?- my_word_calculator(test3,test4).
uncaught exception: error(existence_error(procedure,'.'/3),calculate/0)


any ideas what procedure ./3 might be?
 
I don't know gprolog, but maybe there is a problem with the syntax of your file "In". You should read carefully the explanations for "read_atom".


 
fixed it, this works:

Code:
num(zero,0).
num(one,1).
num(two,2).
num(three,3).
num(four,4).
num(five,5).
num(six,6).
num(seven,7).
num(eight,8).
num(nine,9).
num(ten,10).


plus(X,Y,Z):-
           num(X,NX),
           num(Y,NY),
           Z is NX + NY.

minus(X,Y,Z):-
           num(X,NX),
           num(Y,NY),
           Z is NX - NY.

times(X,Y,Z):-
           num(X,NX),
           num(Y,NY),
           Z is NX * NY.

divide(X,Y,Z):-
              num(X,NX),
              num(Y,NY),
              Z is NX / NY.


my_word_calculator(In,Out):-
                 see(In),
                 tell(Out),

                 calculate,
                 told,
                 seen.
calculate:-
         repeat,
         read_atom(X),
         (X=stop,
          seen,
          told,!;
          read_atom(Y),
          read_atom(Z),
          read_atom(D),
          L=..[Y,X,Z,V],
          call(L),
          write(V),nl,fail).
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top