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:

  1. it's not same zs. initially, when try append([1,2],[3,4],ys)., prolog says "hmm, if original ys in form [x|zs], , need match append(xs, ys, zs)." next time says "hmm, if original zs in form [x|ohterzs], , need match append(xs, ys, otherzs)." imagine eacy recursion call, it's making new variable names.
  2. 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 of ys evaluate truthfully.


Comments

Popular posts from this blog

c# - Better 64-bit byte array hash -

webrtc - Which ICE candidate am I using and why? -

php - Zend Framework / Skeleton-Application / Composer install issue -