confused about Prolog Response -


consider:

bar([], ws, ws). bar([x|xs], ys, [x|zs]) :- bar(xs, ys, zs). 

1) how have no solutions

bar([1,2], [], [x,x]). 

2) how have 1 solution when z=5?

bar([5,y], [y,5], [z,z|_]). 

3)how have 1 solution when z=5 , y = 5?

bar([_,y], [5,5], [z,z,z|_]). 

first, consider meaning of bar/3: rule has 3 arguments representing lists. bar/3 returns solution when last list concatenation of first two:

bar([1,2,3], [4,5], x)       // x=[1,2,3,4,5] bar([1,2,3], x, [1,2,3,4,5]) // x=[4,5] bar(x, [4,5], [1,2,3,4,5])   // x=[1,2,3] 

knowing this, can figure answers questions:

  • bar([1,2], [], [x,x]). needs x unify 1 , 2 @ same time, impossible
  • bar([5,y], [y,5], [z,z|_]). unifies 5 first z, , second z y, single solution
  • bar([_,y], [5,5], [z,z,z|_]). unifies y second z, , third z 5, single solution.

Comments

Popular posts from this blog

jquery - How can I dynamically add a browser tab? -

node.js - Getting the socket id,user id pair of a logged in user(s) -

keyboard - C++ GetAsyncKeyState alternative -