0

i'm a new on Prolog, and i'm already having some problems to understand, the thing is, i was making a test on appending some strings introduced by console:

append_str([],Adder,Adder).
append_str([Head|Tail],Adder,Result):-
    append_str(Tail,[Head|Adder],Result).

sread_str(String):-
    read(String),
    atom(String).

sinput:-
    sinput_str([]).

sinput_str(Lista):-
    sread_str(String),
    sinput_str([String|List]).

sinput_str(List):-
    append_str(List,[],Result),
    nl,
    display(Result),
    nl.

And eventually always getting this output:

|-? sinput.
sinput.
hello.
hello.
world.
world.
9.
9.

'.'(hello,'.'(world,[]))

The number is just for the console to end asking for some more values, i don't know what is wrong, thank you guys in advance.

Diego Jimeno
  • 312
  • 4
  • 15

1 Answers1

0

you should try to understand what happens when you enter a number, forcing sread_str to fail.

Prolog explores all the alternatives available to prove a given goal, and doing so change the variables, undoing such changes when a path fail. Such model it's complicated by side effects required by IO (the read/display builtins).

Then attempt first to have a working sinput_str, something like this

sinput_str([String|List]):-
    sread_str(String),
    sinput_str(List).
sinput_str([]).
CapelliC
  • 59,646
  • 5
  • 47
  • 90