1:- module(onepointfour_basics_stringy_concat,
2 [
3 stringy_concat/3 4 ,stringy_concat/4 5 ]). 6
7:- use_module(library('onepointfour_basics/checks.pl')). 8:- use_module(library('onepointfour_basics/stringy_length.pl')). 9
33
51stringy_concat(ListOfStringy,Result,ResultType) :-
52 stringy_concat(ListOfStringy,Result,ResultType,soft).
60stringy_concat(ListOfStringy,Result,ResultType,Tuned) :-
61 check_that(Result,[break(var),tuned(stringy)],Tuned),
62 check_that(ResultType,[break(var),tuned(stringy_typeid)],Tuned),
63 check_that(ListOfStringy,hard(proper_list)), 64 var_tag(Result,TaggedResult),
65 var_tag(ResultType,TaggedResultType),
66 instantiate_stringy_type(TaggedResult,TaggedResultType), 67 var_tag(ResultType,RetaggedResultType), 68 stringy_concat_2(TaggedResult,RetaggedResultType,ListOfStringy).
69
70stringy_concat_2(var(Result),var(ResultType),ListOfStringy) :-
71 !,
72 stringy_concat_over_list(ListOfStringy,"",TmpResult),
73 (ResultType=atom;ResultType=string), 74 convert_maybe(ResultType,TmpResult,Result).
75stringy_concat_2(var(Result),nonvar(ResultType),ListOfStringy) :-
76 !,
77 stringy_concat_over_list(ListOfStringy,"",TmpResult),
78 convert_maybe(ResultType,TmpResult,Result).
79stringy_concat_2(nonvar(Result),nonvar(ResultType),ListOfStringy) :-
80 !,
81 quick_length_check(ListOfStringy,Result), 82 stringy_concat_over_list(ListOfStringy,"",TmpResult),
83 convert_maybe(ResultType,TmpResult,Result).
84
85stringy_concat_over_list([Stringy|More],RunningString,FinalString) :-
86 check_that(Stringy,hard(stringy)),
87 string_concat(RunningString,Stringy,NewRunningString),
88 stringy_concat_over_list(More,NewRunningString,FinalString).
89stringy_concat_over_list([],String,String).
90
92
93instantiate_stringy_type(var(_Stringy),nonvar(_StringyType)) :- !. 94instantiate_stringy_type(var(_Stringy),var(_StringyType)) :- !. 95instantiate_stringy_type(nonvar(Stringy),var(atom)) :- atom(Stringy),!. 96instantiate_stringy_type(nonvar(Stringy),var(string)) :- string(Stringy),!. 97instantiate_stringy_type(nonvar(Stringy),nonvar(atom)) :- atom(Stringy),!. 98instantiate_stringy_type(nonvar(Stringy),nonvar(string)) :- string(Stringy). 99
100var_tag(X,var(X)) :- var(X),!.
101var_tag(X,nonvar(X)).
102
104
105quick_length_check(ListOfStringy,Result) :-
106 stringy_length(Result,MaxLength),
107 quick_length_check_2(ListOfStringy,0,MaxLength).
108
109quick_length_check_2([S|More],RunningLength,MaxLength) :-
110 stringy_length(S,AddLength), 111 NewRunningLength is RunningLength + AddLength,
112 NewRunningLength =< MaxLength,
113 quick_length_check_2(More,NewRunningLength,MaxLength).
114
115quick_length_check_2([],Length,Length). 116
123
124convert_maybe(atom,In,Out) :- atom(In) ,!,In=Out.
125convert_maybe(atom,In,Out) :- string(In),!,atom_string(Out,In).
126convert_maybe(string,In,Out) :- atom(In) ,!,atom_string(In,Out).
127convert_maybe(string,In,Out) :- string(In),!,In=Out
Simply concatenate stringys
Homepage for this code
https://github.com/dtonhofer/prolog_code/blob/main/unpacked/onepointfour_basics/README_stringy_concat.md
*/