Why [] = _. true in Prolog?

I'm learning Prolog and don't understand why ?- [] = _.

SWI-Prolog returns true. []

means an empty list, and _

means that it is not empty, right?
Can anyone explain this logic?

+3


source to share


1 answer


_

is a boolean variable, just like X

or anything else that starts with an underscore or an uppercase letter. Free variables (i.e. Variables that are not yet associated with any member) can be combined with anything. A purpose like this [] = X

means "unify X

with []

", whereby, if done, each usage X

would refer to the term []

.

C _

as a variable is the same as for X

, except that it _

is an anonymous variable: it cannot be reused, its name does not matter, and different occurrences _

refer to different variables. Therefore, _

it can never be attached before meeting the target [] = _

. So this union succeeds, which is why you get the answer true

.



_

in itself does not mean "not empty". But you might be confused about using it as a placeholder: L = [_,_,_]

means it L

is a list of three elements (which we don't know anything about). In this sense, it _

means "there is something here." But it must be inside the list for this value.

+9


source







All Articles