How to make list out of objects in Prolog? -
i can add single object list code , query:
makelist(a, b, c, x) :- append([1, 2, 3],a, x). ?- makelist(a, b, c, x). x = [1, 2, 3|a].
however rather usual separator comma (,) there's vertical line separator (|) , cannot add object same list:
makelist(a, b, c, x) :- append([1, 2, 3],a, x), append(x,b, x). ?- makelist(a, b, c, x). false.
there several misunderstandings. first, lists , elements confused leads "dotted pair" |a]
. then, seems role of variables not clear you.
in goal append([1,2,3],a,x)
both a
, x
supposed lists. however, set a
a
not list. problem behind append/3
accepts term second argument. see this, @ answers:
| ?- append([1,2,3],a,x). x = [1,2,3|a].
so a
can anything, should rather list.
| ?- = a, append([1,2,3],a,x). = a, x = [1,2,3|a].
note append/3
insists first argument list (or partial list). once have bad list, can no longer append further. is:
| ?- = a, append([1,2,3],a,x), append(x, _, _). no
note did not use definition literally. instead, replaced arguments _
since cannot help.
the second problem stems goal append(x, b, x)
. goal, can only true if b = []
. let's try out:
| ?- append(x, b, x). x = [], b = [] ? ; x = [_a], b = [] ? ; x = [_a,_b], b = [] ? ; x = [_a,_b,_c], b = [] ...
and:
| ?- b = [_|_], append(x, b, x).
loops.
while there many answers x
, b
[]
.
Comments
Post a Comment