N.W.
N.W.

Reputation: 373

Can the head of a list in prolog consist of multiple elements?

For example if I have a list [1, 2| 3], would the head be 1, 2 and the tail 3? Or would the head still be 1 and the tail 2, 3?

Upvotes: 3

Views: 1428

Answers (1)

willeM_ Van Onsem
willeM_ Van Onsem

Reputation: 477608

Note: [1, 2| 3] is not a list; [1, 2 | [3]] is.

Short answer: there is only one element in the head, [1, 2| [3]] is syntactical sugar for [1 | [2 | [3]]], so it is a list [1, 2, 3].

Prolog's lists are like Lisp lists: they consist out of two possible values: an empty list [], and a "cons" [H|T] with H the *head, and T the tail.

Most Prolog interpreters will however allow syntactical sugar like [E1, E2, E3] and [E1, E2, E3 | T]. to make it more convient to do list processing.

Upvotes: 2

Related Questions