在Prolog中实现模拟连接



注意:这是一个分配问题。我已经被这个问题困扰了好几个小时了。简单地说,"基本上是导致代码行为不端。

预期输出

% ?- read_command(L).
% |: cal -n -r file1 file2
% L = concatenate([-n,-r],[file1,file2]).

观察到的输出不正确

% ?- read_command(L).
% |: cal -n -r file1 file2
% L = concatenate([], ['-n', '-r', file1, file2]).

我不知道-r是如何变成"-r"和-n是"-n"的。

read_command(L1):-             
   get0(C),
   read_command(_, L, C),name(X, L),atomic_list_concat([M1|T1],' ',X),
   ([M1] == [cat]
   ->cmd_cat([M1|T1],L1);
   L = L1).
minus_stripped(-X, X).
cmd_cat([_|T],Z):-
writeln(T),
divide_dashed(T,X,Y),writeln(X),writeln(Y),maplist(minus_stripped, X, Xs),
Z = concatenate(Xs,Y).
divide_dashed([],[] , []).
divide_dashed([-E|R], [-E|Ds], Ps) :- !, divide_dashed(R, Ds, Ps).
divide_dashed([E|R], Ds, [E|Ps]) :- divide_dashed(R, Ds, Ps).

再次感谢您的帮助。

divide_dashed(L, D, P) :-
    partition(dashed, L, D, P).
dashed(S) :- atom_concat(-,_,S).

或者,如果您正在运行SWI-prolog的最新版本,其中库(yall)可用:

divide_dashed(L, D, P) :-
    partition([S]>>atom_concat(-,_,S), L, D, P).

最新更新