Prolog: copy list to already assigned list -
i struggling feel should simple, doesn't seem in prolog! make list2 equal list1, list2 has been assigned in past (i.e. not variable).
if list2
variable, unify list1
, list2
:
list2=list1.
but in case list2
exists (and not equal list1
) , predicate fails.
i have tried following:
copy(list1,list2). copy(l,r) :- ccp(l,r). ccp([],[]). ccp([h|t1],[h|t2]) :- ccp(t1,t2).
but fails, presumably same reason?
could steer me in right direction? teaching myself prolog apologies if overlooking simple. thanks.
context:
i trying add term l
list list2
each time predicate add_to_list
run. far have following:
add_to_list(list2,list1):- generatel(l), ( \+ memberchk(l,list2) -> list1=[l|list2] ; list1 = list2).
so hoping there way reassign list2 equal list1, perhaps not prolog way of thinking.
instead, have attempted pass through list1 follows:
my_predicate(list1,listout):- add_to_list(list1,listout), write(listout),nl,nl. add_to_list(list2,list1):- generatel(l), ( \+ memberchk(l,list2) -> list1=[l|list2] ; list1 = list2).
this works when call my_predicate
follows: my_predicate([],listout), i.e. list1 empty (as start with). able call my_predicate
subsequent times my_predicate(list1,listout)
pre-existing list1
taken input.
you can not "modify" variable after bound. see example:
forcing variable reassign (prolog)
prolog: changing variable between 2 known values consecutivly
when executing rules "copy" or "ccp" on 2 bound variables, prolog verify if equals , fail if not, not reassign. if of them not bound, made them equal. if of them partially unbound (some list items unbound) made equal unbound items , verify remainders.
if present full context of problem, suggest alternatives.
Comments
Post a Comment