listsyntaxprologoperatorsswi-prolog# What exactly is the vertical slash function in PROLOG? Is it an operator?

while reading the documentations and testing examples of SWI PROLOG implementation I began to go deeper into lists. PROLOG lists are no different from most languages: it has a Head and a Tail.

I then learned that SWI PROLOG lists can be expressed like this:

`[ Head | Tail ]`

The syntax is pretty simple, square brackets with a head and a tail, separated by a vertical slash `|`

.

Then I started wondering: what is the meaning (the semantics) of the vertical slash `|`

in PROLOG?

I know that the vertical slash is indeed a special character, but why does it necessarily have to be a vertical slash? Is it an operator? Is it used for system or language (meta) applications? What is its specific function in the language?

Solution

Yes, `|`

is a right-associative infix operator of precedence 1105, *right-associative* meaning that an expression like

```
a|b|c|d
```

binds as

```
'|'( a , '|'( b , '|'( c , d ) ) )
```

rather than the left-associative binding

```
'|'( '|'( '|'( a , b ) , c ) , d )
```

It is part of Prolog's syntactic sugar for list notation. In Prolog, any non-empty list, has a single item that is denoted as its * head*, and the remainder of the list, itself another list (which may be empty), denoted as the

So one can easily partition a list into its *head* and *tail* using `|`

. So

```
[Head|Tail] = [a,b,c,d]
```

results in

```
Head = a
Tail = [b,c,d]
```

From my answer here,

Prolog's list notation is

syntactic sugaron top of very simple prolog terms. Prolog lists are denoted thus:

The empty list is represented by the atom

`[]`

. Why? Because that looks like the mathematical notation for an empty list. They could have used an atom like`nil`

to denote the empty list but they didn't.A non-empty list is represented by the term

`.\2`

, where the first (leftmost) argument is theheadof the list and the second (rightmost) argument is thetailof the list, which is, recursively, itself a list.Some examples:

An empty list:

`[]`

is represented as the atom it is:`[]`

A list of one element,

`[a]`

is internally stored as`.(a,[])`

A list of two elements

`[a,b]`

is internally stored as`.(a,.(b,[]))`

A list of three elements,

`[a,b,c]`

is internally stored as`.(a,.(b,.(c,[])))`

Examination of the head of the list is likewise syntactic sugar over the same

`./2`

notation:

`[X|Xs]`

is identical to`.(X,Xs)`

`[A,B|Xs]`

is identical to`.(A,.(B,Xs))`

`[A,B]`

is (see above) identical to`.(A,.(B,[]))`

- Doing math to a list in python
- Find maximum value and index in a python list?
- How can I manually (with an explicit loop) find the index of the maximum value in a list?
- How to find all positions of the maximum value in a list?
- Transpose list of lists
- Transpose/Unzip Function (inverse of zip)?
- Why does Python return None on list.reverse()?
- Why is `tranpose [[]] == []` in Haskell?
- C# List with Unique data based on the Date
- Java 8 Streams remove duplicate entries, keeping the element with the min date
- Pop multiple items from the beginning and end of a list
- How to get the first value in a Python dictionary
- Inheriting from List<T>
- vim: How to search/replace in all opened files?
- Checking to see if a list of lists has equal sized lists
- Parse yaml into a list in python
- Splitting a list into N parts of approximately equal length
- How to traverse a base R list vertically/by rows using R's characteristics
- Create a program to ask a number and print all divisors
- Apply function to one element in a list of lists
- python get first x elements of a list and remove them
- list cbind with two list element in R
- Non-alphanumeric list order from os.listdir()
- GetValueOrDefault or null-coalescing for Dictionary or List
- Convert one element in list to uppercase in Python
- material ui: how to change fontSize in Lists?
- How to find all occurrences of an element in a list
- image from uint16 array from serial port
- Check if string does not contain strings from the list with wildcard when % symbol in the list value
- Return a certain count in multi dimensional list?