I have, for example this number list:
[1,2,3,4,5,6,7,8,9,10].
I have to search a number for example 7
and insert "$"
BEFORE and AFTER 7
:
[1,2,3,4,5,6,$,7,$,8,9,10].
How can i do this?
I have, for example this number list:
[1,2,3,4,5,6,7,8,9,10].
I have to search a number for example 7
and insert "$"
BEFORE and AFTER 7
:
[1,2,3,4,5,6,$,7,$,8,9,10].
How can i do this?
I use swi prolog, you could use a replace predicate (found in other stack overflow questions), combined with a flatten operator on a list:
:- use_module(library(lists)).
replace(_, _, [], []).
replace(O, R, [O|T], [R|T2]) :- replace(O, R, T, T2).
replace(O, R, [H|T], [H|T2]) :- H \= O, replace(O, R, T, T2).
now:
replace(1,[$,1,$],[4,3,2,1],NewList),flatten(NewList,FlattendList).
NewList = [4, 3, 2, [$, 1, $]],
FlattendList = [4, 3, 2, $, 1, $] ;
Of course you can build pretty predicate for that.