/* A library of useful LPA routines using built-in LPA predicates */


/* 'str2list/3' turns a string(arg1) containing "separators"(arg2) into a list, while eliminating "white-space characters" as well, so that the output-list does not contain unnecessary spaces. If the "separator" is chosen to be a space, then the output-list becomes a list of words contained in the input;
if any another separator is  chosen, "most" spaces are again removed from the input... */

/* test:
| ?- str2list(`  this  sentence has   too many spaces `,` `,X).
X = [`this`,`sentence`,`has`,`too`,`many`,`spaces`]
*/

str2list(Si,Sep,SLx):- nonvar(Si), strscan(SLx,Sep) <~ Si.

strscan([],_):- skip_layout, eof, !.
strscan([S|SL],Sep):- find(Sep,2,Sfx) ~> S, !, strscan(SL,Sep).



/* del_all_subs/3 deletes all occurrences of a substring (arg2) in a string(arg1). It is (yet) another example of find/3's usefulness (a built-in LPA Prolog predicate): */

del_all_subs(SOURCE,SubStr,STRx):-
 ((repeat,find(SubStr,2,Fndx),eof,!) <~ SOURCE) ~> STRx, !.



addvars/4  is called with arg2 = [], and arg1 = an atom-list containing _some_variables_ (signified by capital initial letters) and "constants" (=anything else); it then produces a new list (arg3) which is a copy of the original (arg1) except that all "variables" have been replaced by authentic (PROLOG-) variables, while arg4 is bound to a list of these variables in pairs of the form [ (ConstantAtomName-VARIABLE)... ] (exactly the same format as used by LPA Prolog's 'vars/2' - predicate); it also sorts the variables (removing duplicates) using sort/2 (a fast LPA Prolog built-in predicate): */

% NOTE: In this particular implementation, the variables are also
% turned into lower-case- atoms; This is because we assume
% that the user "knows" the special atoms signifying facts
%       (e.g. 'colour', 'brush' are "LPA-meaningful") and uses the
%       names of such known entities, with capital initial letters
%       to signify variables (of a known type) within each query:

addvars([],VarsList,[],VarsListX):- sort(VarsList,VarsListX), !.
addvars([V|L],VARS,[NEW|Lx],VLx):- V @=< 'Z', %general "compare"
 lwrupr(V1,V), % replace with "V1=V" for no case-change
 add_var((V1,NEW),VARS,Vnew), !, addvars(L,Vnew,Lx,VLx).
addvars([Const|L],VARS,[Const|Lx],Vx):- !, addvars(L,VARS,Lx,Vx).

add_var((CONST,Var),Old,Old):- member((CONST,Var),Old), !.
add_var((CONST,Var),Old,[(CONST,Var)|Old]):- !.

/* test call:
| ?- addvars([this,'X',isa,good,'X',and,'Y',isa,'Y'],[],X,VL).

X = [this,_3452414,isa,good,_3452414,and,_3452602,isa,_3452602] ,
VL = [('Y',_3452602),('X',_3452414)]

| ?- 
*/



% strl2atoms/2 is simple but useful, converting LPA atoms to LPA strings -and vice versa:

strl2atoms([],[]):- !.
strl2atoms([S|SL],[A|AL]):- atom_string(A,S), !, strl2atoms(SL,AL).
 


% 'scan_sentence/3' takes a user-input(string) and turns it into
% an atom-list (which also contains "true-Prolog-"variables). It
% also produces a list of these "true-Prolog-"variables (arg3) as
% pairs of the form [ (ConstantAtomName-VARIABLE)... ] (exactly
% the same format as used by LPA Prolog's 'vars/2' - predicate);

scan_sentence(String,AtomListWithVARS,VARlistX):-
   str2list(String,` `,SList), strl2atoms(SList,AtomList),
   addvars(AtomList,[],AtomListWithVARS,VARlistX).
 
 


OTHER PAGES in this site:

"What's better than a good plate of spaghetti? Well... two good plates of spaghetti!"

-software advice from a TV ad of the Greek Spaghetti Industry