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 originalysin form[x|zs], , need matchappend(xs, ys, zs)." next time says "hmm, if originalzsin 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
zssince query
append([1,2],[3,4],ys)., ,ysvariable, prolog interprets query asking match ofysevaluate truthfully.
Comments
Post a Comment