1

我意识到我的第一个问题太长了,没有人能理解。

这是一个浓缩的更简单问题的链接,该问题在更短的程序中展示了我的问题。

[精简问题链接] Prolog - 未返回所需值

==================================================== =================================

我正在尝试在 Prolog 中编写一个 Brainf*ck 解释器。

函数 bf 将采用 3 个字符串。仅相关部分代码如下:

%Prog - string of BF program
%Input- string of inputs for the BF program
%Output - what the BF program will output

bf(Prog, Input,Output):- run(Prog, Input, Output).

run(Prog, Input, Output) :-
    create_mem(Mem),
    iterateBF(Prog,[],Mem,0, Input, Output).

%start by creating the memory space
create_mem(Memory) :-
    var(Memory), %check if it is uninstantiated variable
    create_mem(Memory,10000).

create_mem([],0) :- !.
create_mem([0|Tail],Size) :-
    NewSize is Size - 1,
    create_mem(Tail,NewSize).

%get memory value at ptr location
get_mem([Value|_],Value,0). %base case
get_mem([_|Tail],Value,Ptr) :-
  Ptr > 0,
    NewPtr is Ptr - 1,
    get_mem(Tail,Value,NewPtr).

%for adding a value to memory space
%need to mod with 256 due to the it being 8 bits to get the actual value.

increase_mem([Value|MemTail],[NewValue|MemTail],0) :-
    NewValue is (Value + 1) mod 256.
increase_mem([Head|Tail],[Head|NewTail],Ind) :-
    Ind > 0,
    NewInd is Ind - 1,
    increase_mem(Tail,NewTail,NewInd).

%iterateBF(Prog,[],Mem,0, Input, []).
%when reach the end of the the

iterateBF([],_,_,_,_,[]) :- !.
iterateBF([],_,_,_,_,_) :-!.
iterateBF(Instruction,Inst_Iterated,Mem,Ptr,Input,Output) :-

   checkInstruction(Instruction,Inst_Iterated,Mem,Ptr,Input,Output,
      NewInstruction,NewInst_Iterated,NewMem,NewPtr,NewInput,NewOutput),
    iterateBF(NewInstruction,NewInst_Iterated,NewMem,NewPtr,NewInput,NewOutput).


checkInstruction([Plus|ProgTail],Inst_Iterated,Mem,Ptr,Input,Output,ProgTail,Inst_Iterated,NewMem,Ptr,Input,Output) :-
        char_code('+',Plus),
        increase_mem(Mem,NewMem,Ptr), !.

%get the memory value at ptr
%Put into Output
checkInstruction([Period|ProgTail],Inst_Iterated,Mem,Ptr,Input,Output,ProgTail,Inst_Iterated,Mem,Ptr,Input,NewOutput) :-
    char_code('.',Period),
    NewOutput = [Output|Value],
    get_mem(Mem,Value,Ptr),!.

上面的代码将编译并且仅适用于:

bf("+.","",Out).

SWI-Prolog 只会返回true,而我希望它返回Out.

在 SWI 中跟踪它,只要它返回到iterateBF(Prog,[],Mem,0, Input, Output)in的调用runOutput就会丢失其先前的值。

您只需要注意以下功能:

  1. 迭代BF
  2. 检查说明

其余的只是为了编译。

4

1 回答 1

1

checkInstruction([Plus|ProgTail],Inst_Iterated,Mem,Ptr,Input,Output,ProgTail,Inst_Iterated,NewMem,Ptr,Input,Output)的太复杂了。

实际上,您的问题在于Output没有正确绑定。但是,与其进行“平庸”的更正,不如进行总体改进:

尝试评论您的代码,您期望它会做什么,并在完成后尽可能简化它。

于 2012-12-02T15:28:56.230 回答