Prolog list concat -
i learning prolog , have stumbled upon piece of code , cannot make sense of it:
code:
append([],ys,ys). append([x|xs],ys,[x|zs]):- append(xs, ys, zs).
in example, instruction given is:
code:
append([1,2],[3,4],ys).
and output ys = [1,2,3,4]
and don't understand how happens.
i understand values on xs removed 1 one until equals [], don't understand 1. why zs being changed; if fact append([],ys,ys)
, zs
should equal [3,4]
why changing in way if obeys fact?
2. how [1,2]
in beginning of zs
??
i used trace , result of more confuse, because zs
values _g5166
, _g5112
, etc... values? memory addresses?
and why that, if don't use write(), console displays ys = ...
? , why ys
, not xs
or zs
?
regarding first pair of questions:
- it's not same
zs
. initially, when tryappend([1,2],[3,4],ys).
, prolog says "hmm, if originalys
in form[x|zs]
, , need matchappend(xs, ys, zs).
" next time says "hmm, if originalzs
in form[x|ohterzs]
, , need matchappend(xs, ys, otherzs).
" imagine eacy recursion call, it's making new variable names. - the 1 , 2 front through first 2 invocations, each 1 own version of
zs
.
regarding other points:
the strange thingies trace these recursion-local
zs
since query
append([1,2],[3,4],ys).
, ,ys
variable, prolog interprets query asking match ofys
evaluate truthfully.
Comments
Post a Comment