Q:

Prolog character stream error

My code is supposed to stop when it finds stop in the file its reading from, but its not. I keep getting an error:
% reads in a character and then checks whether this character is a blank,
% a carriage return or the end of the stream. In any of these cases a
% complete word has been read otherwise the next character is read.

calculate([stop],_) :- !.
calculate([],_):-!.
calculate([Word|Rest],X) :-
    word_to_number(Word,Symbol),
    concat(X,Symbol,NewX),
    calculate(Rest,NewX),
    atom_to_term(NewX,Eq,[]),
    print('Calculating '),print(NewX),print('  The result is: '),
    Result is Eq,
        print(Result),nl,
    execute.

Any help would be greatly appreciated!

A:

The problem is that calculate is recursive. At some point, calculate([one], '03+') called, which in turn calls calculate([], '03+1'), which gives a result (4). It then invokes execute and processes the rest of the input.
Then, the calling calculate succeeds, and now goes on to applying atom_to_term to '03+', which gives the error.
You can fix this by moving the conversion to an atom into a separate predicate:
to_atom([Word], Symbol) :- word_to_number(Word, Symbol).
to_atom([Word|Rest], Term) :-
    word_to_number(Word,Symbol),
    to_atom(Rest, Symbol2),
    concat(Symbol,Symbol2,Term).

...
calculate(List) :-
    to_atom(List, NewX),
    atom_to_term(NewX,Eq,[]),
    ...

Then you won't need the dummy 0 in the beginning, either.

