Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

list

prolog

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?

like image 783
N.W. Avatar asked Jan 28 '26 06:01

N.W.


1 Answers

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.

like image 86
Willem Van Onsem Avatar answered Jan 31 '26 04:01

Willem Van Onsem



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!