Here is a simple Prolog program to exhibit a problem that I currently have with my real code.
I am trying to write a Brainf*ck interpreter in Prolog, but it is not returning the right Output
I am running bf("+.", Output)
Expected Return - Output = [1]
Actual Return - Output = []
.
Output
looses its value when it returns to iterateProg(Prog,Output)
in bf2
.
Program is as follows:
bf2(Prog,Output):-
iterateProg(Prog,Output).
iterateProg([],_):- !.
iterateProg(Prog, Output):-
checkInstruction(Prog,Output, NewProg, NewOutput),
iterateProg(NewProg, NewOutput).
checkInstruction([Plus|ProgTail],Output,ProgTail,Output):-
char_code('+',Plus)
%increase memory by 1 for printing later.
.
checkInstruction([Period|ProgTail], Output, ProgTail, NewOutput):-
char_code('.',Period),
%take the value in memory from addition just now, we will assume it is 1
append(Output,[1],NewOutput).
Your predicate
bf2(Prog, Output)
has the meaning of: this programProg
correspond with this outputOutput
- so theOutput
in thebf2
predicate and also initerateProg
should contains the full output.So in
checkInstruction
predicate, the first 2 arguments should contain the full program and the full output, and the last 2 arguments should contain the partial program and the corresponding partial output.Therefore, you should append the
[1]
withNewOutput
, to produce the final output inOutput
for the second rule ofcheckInstruction
predicate. You should rename theNew...
with a different name - since it is actually the partial/"tail" program and corresponding output.Note the order (I was inaccurate in the comment).
[1]
is the output for the current instruction, and it should precede the output of the instruction inProgTail
.By the way,
bf2
predicate is unnecessary - since it calliterateProg
predicate with exact same arguments.Another thing is
iterateProg([],_)
should beiterateProg([],[])
since an empty program should correspond with empty output. (The converse is not true, so you should never put a cut!
here, if you have learned about it).