简体   繁体   中英

Implementing Mock Concatenate in Prolog

Note: This is an assignment question. I have been stuck on this for hours. To simply put it a '' is basically causing the code to misbehave.

Expected Output

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

Incorrect Output Observed

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

I am not sure how -r came to be '-r' and -n as '-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).

And again thank you for all the help.

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

or, if you're running a recent version of SWI-prolog, where library(yall) is available:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM