|Did you know ...||Search Documentation:|
:- use_module(library(lists)).(can be autoloaded)
|ListOfLists||must be a list of possibly partial lists|
?- append([[1,2], ], List). List = [1,2,3].
The append/3 predicate can be used to split and join lists. We can combine that to realise substituting all appearances of a sublist into another as illustrated below.
append(This, After, Rest)creates a pattern from This, i.e., the list Rest starts with This.
append(Before, Rest, MyStr)matches the pattern against the input list. On a match we commit (!). Now Before is the input list before the match and After is the input list after the match.
subst(This, That, MyStr, Result) :- append(This, After, Rest), append(Before, Rest, MyStr), !, subst(This, That, After, AfterResult), append([Before,That,AfterResult], Result). subst(_, _, S, S).
?- portray_text(true). true. ?- subst(`this`,`that`,`athishellothis`,R). R = `athathellothat`.