Reputation: 313
I want to add a element to a list and replace it with my list.
So i wrote
initialstate(0,[],[[1,0],[2,3],[1,2],[1,3]]).
add(X,L2,[X|L2]).
I would like to make something like that...
?- initialstate(_,List,_),add(4,List,List)
and replace me the initialstate to this one
initial_state(0,[4],[[1,0],[2,3],[1,2],[1,3]]).
The maining question for me it's how to replace the list inside the indetifiers "initialstate".
I am new in prolog...Please help me... and sorry if it is something stupid.
Upvotes: 0
Views: 147
Reputation: 60014
once provided the appropriate declaration
:- dynamic initialstate/3.
you could update your DB in this way
?- retract(initialstate(A,B,C)), assertz(initialstate(A,[4|B],C)).
Upvotes: 1
Reputation: 14873
If you want to change a list you get a different list. So you need a different name for it.
Prolog is not an imperative language but more a descriptive one. This means: you do not say do this and this. You say this is like that and when this is the case this is true/false in files (knowledge). Then you start the interpreter, consult the knowledge and ask questions wether something is true.
Writing A = B means that A is always B.
?- initialstate(_,List,_),add(4,List,List)
means initialstate of a List is when 4 added to the list is the list itself.
This is not true
. if 4 is added there should be a 4 where no 4 was before.
You can do this:
append4(List, ListWithFourInside) :- append(List, [4], ListWithFourInside).
Which reads like when I append 4 to a list I get a list with 4 inside.
It can be done shorter if you want the 4 in the beginning
add4(List, ListWithFourInside) :- [4 | List] = ListWithFourInside.
or even shorter:
add4(List, [4|List]).
Upvotes: 0