Information Technology Reference
In-Depth Information
Here are some examples of pairs of lists that do unify:
[]
and
[]
[a,b,c]
and
[a,b,c]
[X]
and
[a]
with
X=a
[a,b,X]
and
[Y,b,Y]
with
X=a
,
Y=a
[X,X]
and
[[Y,a,c],[b,a,c]]
with
X=[b,a,c]
,
Y=b
[[X]]
and
[Y]
with
X=_G237
,
Y=[_G237]
[a,b,c]
and
[a|[b,c]]
[a,b,c]
and
[a|[b|[c]]]
[X|Y]
and
[a]
with
X=a
,
Y=[]
[a|[b,c]]
and
[a,X,Y]
with
X=b
,
Y=c
[a,b,c]
and
[X|Y]
with
X=a
,
Y=[b,c]
[X,Y|Y]
and
[a,[b,c],b,c]
with
X=a
,
Y=[b,c]
Lists will not unify if they have different numbers of elements or if at least one
corresponding element does not unify. Here are some nonunifying pairs:
[a]
and
[]
[a,b,c]
and
[a,a,c]
[]
and
[[]]
[X,Y]
and
[U,V,W]
[a,b,c]
and
[X,b,X]
[X|Y]
and
[]
Here are some useful conclusions to draw from these examples:
X
matches anything, including any list.
[X]
matches any list with exactly one element.
[X|Y]
matches any list with at least one element.
[X,Y]
matches any list with exactly two elements.
7.2 Writing programs that use lists
Programs that go through lists usually end up being
recursive
, since it may not be
known in advance how many elements are involved. Each list will have
some number
n
of elements. This is just like the recursion discussed in section 4.3, where a block that