Not logged in. · Lost password · Register

EinGartenzwerg
Member since Oct 2018
10 posts
Subject: append() Verständnisproblem
Hi,

ich habe bei der append funktion, wie sie in der VL definiert wurde leider noch etwas Verständnisprobleme:
Wenn ich jetzt
append([X|R],L,[X|S]):−append(R,L,S).
habe, schaut dann Prolog nicht ob die beiden X gleich sind?  S ist doch die Ergebnisliste, die eigentlich noch leer ist o.O oder wie interpretiert es das dann?

mfg
rappatoni
Member since Nov 2018
26 posts
The answer depends on how the function is called. If the query is for example "?- append([1],L,[2,1])" then Prolog will try to match this query to head of "append([X|R],L,[X|S]):−append(R,L,S).", fail and return false (as intended, as there is no way to get the list [2,1] by appending [1] to any list L).

If on the other hand the query is "?- append([1],[2,1],Y)", Prolog will successfully match this with "append([X|R],L,[X|S])", substitute 1 for X and by backchaining obtain the new query/proof goal "?- append([],[2,1],S)" which it in turn matches successfully to the first clause of the function (append([],L,L).). Hence S=[2,1] and [X|S]=[1,2,1] as desired.


Remember that Prolog takes the query as a proof goal and works backwards from it. It attempts step by step (generate and test) to come up with a variable assignment and backtracks if an assignment fails. Maybe it helps to change your mental model of [X|S] as an "still empty result list" into which things should be put. Prolog works the other way round: the list [X|S] is not an initial state but a goal which it tries to obtain by assigning variables and testing them.
EinGartenzwerg
Member since Oct 2018
10 posts
Oh okay! O.O

so
the list [X|S] is not an initial state but a goal which it tries to obtain by assigning variables and testing them.
this really helped me! In other words: If there is not yet a List but A Variable (Y) Prolog tries to get to a state (by recursively replacing the querey) where it can assign the already known X to [X|S]. But it all depends oh how you call the function.

Thank you!
Close Smaller – Larger + Reply to this post:
Verification code: VeriCode Please enter the word from the image into the text field below. (Type the letters only, lower case is okay.)
Smileys: :-) ;-) :-D :-p :blush: :cool: :rolleyes: :huh: :-/ <_< :-( :'( :#: :scared: 8-( :nuts: :-O
Special characters:
Go to forum
Datenschutz | Kontakt
Powered by the Unclassified NewsBoard software, 20150713-dev, © 2003-2011 by Yves Goergen