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, impossiblebar([5,y], [y,5], [z,z|_]).
unifies 5 first z, , second z y, single solutionbar([_,y], [5,5], [z,z,z|_]).
unifies y second z, , third z 5, single solution.
Comments
Post a Comment