Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- % separators
- separator(" ").
- separator(",").
- separator(";").
- separator("|").
- % operators
- safe_op(string_upper).
- safe_op(string_lower).
- safe_op(string_length).
- safe_op(number_string).
- safe_op(reverse).
- safe_op(str_split(Sep)) :- separator(Sep).
- safe_op(str_join(Sep)) :- separator(Sep).
- map_op(maplist(Op)) :- safe_op(Op).
- known_op(X) :- safe_op(X).
- known_op(X) :- map_op(X).
- % Check whether Input can become Output in N steps by
- % using the list of code in the last argument.
- %
- % ?- run_test("I am Robert","Robert am I",3,X).
- % X = [str_split(" "), reverse, str_join(" ")] .
- run_test(Input,Input,0,_).
- run_test(_,_,0,_) :-
- !,fail. % we ran out of steps
- run_test(Input,Output,1,[HCode]) :-
- known_op(HCode),
- catch(call(HCode,Input,Output),_,fail).
- run_test(Input,Output,Nsteps,[HCode|TCode]) :-
- known_op(HCode),
- catch(call(HCode,Input,Intermed),_,fail),
- NewNsteps is Nsteps - 1,
- run_test(Intermed,Output,NewNsteps,TCode).
- % ?- run_test("a|b|c",["a","b","c"],1,[str_split("|")]).
- % true .
- str_split(Sep,In,Out) :-
- split_string(In,Sep,"",Out).
- % ?- run_test(["a","b","c"],"a|b|c",1,[str_join("|")]).
- % true .
- str_join(_,[H|[]],Out) :-
- !,H = Out.
- str_join(Sep,[H|T],Out) :-
- string_concat(H,Sep,HeadAndSep),
- str_join(Sep,T,StringRest),
- string_concat(HeadAndSep,StringRest,Out).
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement