It's because the form [Head|Tail]
is just syntactic sugar for '.'(Head, Tail)
(everything is a term and lists are no exception). Normally, for the list made of 1
, 2
, 3
and 4
, you'd have to write
'.'(1, '.'(2, '.'(3, '.'(4, [])))).
As you can see, that's not very practical and instead we use the shortcut:
[1|[2|[3|[4|[]]]]]
And this shortcut has a shortcut:
[1, 2, 3, 4]
And you can mix those as you wish:
[1, 2|[3, 4]]
That is handy when you want to specify some elements and then let the tail free:
[1, 2|A]
BTW, you can see that for yourself by using write_canonical/1
:
?- write_canonical([1, 2, 3, 4]).
'.'(1,'.'(2,'.'(3,'.'(4,[]))))
true.
Hope it helped