(1)
edit :- edit(file([],[])).
edit(File) :-
write_prompt, read(Command), edit(File,Command).
edit(File,exit) :- !.
edit(File,Command) :-
apply(Command,File,File1), !, edit(File1).
edit(File,Command) :-
writeln([Command,'is not applicable']), !, edit(File).
apply(up,file([X|Xs],Ys),file(Xs,[X|Ys])).
apply(up(N),file(Xs,Ys),file(Xs1,Ys1)) :-
N > 0, up(N,Xs,Ys,Xs1,Ys1).
apply(down,file(Xs,[Y|Ys]),file([Y|Xs],Ys)).
apply(down(N),file(Xs,Ys),file(Xs1,Ys1)) :-
N > 0, down(N,Xs,Ys,Xs1,Ys1).
apply(insert(Line),file(Xs,Ys),file(Xs,[Line|Ys])).
apply(delete,file(Xs,[Y|Ys]),file(Xs,Ys)).
apply(delete(N),file(Xs,Ys),file(Xs1,Ys1)) :-
N > 0, delete(N,Xs,Ys,Xs1,Ys1).
apply(print,file([X|Xs],Ys),file([X|Xs],Ys)) :-
writeln(X).
apply(print(*),file(Xs,Ys),file(Xs,Ys)) :-
reverse(Xs,Xs1), write_file(Xs1), write_file(Ys).
apply(search(T), file([X|Xs],Ys), file(Xs1,Ys1)) :-
search(Xs,T,N), apply(up(N),file([X|Xs],Ys),file(Xs1,Ys1)).
apply(search(T), file(Xs,Ys), file(Xs1,Ys1)) :-
search(Ys,T,N), apply(down(N),file(Xs,Ys),file(Xs1,Ys1)).
apply(search(T), file([T|Xs],Ys), file([T|Xs],Ys)).
apply(replace(S,T),file(Xs,Ys),file(Xs1,Ys1)) :-
replace(Xs,Xs1,S,T), replace(Ys,Ys1,S,T).
up(N,[],Ys,[],Ys).
up(0,Xs,Ys,Xs,Ys).
up(N,[X|Xs],Ys,Xs1,Ys1) :-
N > 0, N1 is N-1, up(N1,Xs,[X|Ys],Xs1,Ys1).
down(N,Xs,[],Xs,[]).
down(0,Xs,Ys,Xs,Ys).
down(N,Xs,[Y|Ys],Xs1,Ys1) :-
N > 0, N1 is N-1, down(N1,[Y|Xs],Ys,Xs1,Ys1).
delete(N,Xs,[],Xs,[]).
delete(0,Xs,Ys,Xs,Ys).
delete(N,Xs,[Y|Ys],Xs1,Ys1) :-
N > 0, N1 is N-1, delete(N1,Xs,Ys,Xs1,Ys1).
replace([],[],_,_).
replace([X|Xs],[X|Ys],S,T) :- X \= S, replace(Xs,Ys,S,T).
replace([S|Xs],[T|Ys],S,T) :- replace(Xs,Ys,S,T).
search([X|Xs],X,1).
search([X|Xs],Y,N) :-
Y \= X, search(Xs,Y,M), N is M+1.
write_file([X|Xs]) :-
write(X), nl, write_file(Xs).
write_file([]).
write_prompt :- writeln('>>').
0 件のコメント:
コメントを投稿